8 things you must know about roblox humanoid

8 things you must know about Roblox Humanoid

The Roblox Humanoid object gives a model the functionality of a character that players can control. This is not just limited to player models but can also be used for other player controllable objects or even NPCs.

What you’ll learn

  • Useful properties of Roblox Humanoid
  • How to change player speed
  • How to control player’s jumping
  • Give player’s accessories
  • Use player emotes
  • Affect how player’s take damage
  • React to player movement events
  • How to use player tools
  • Control player’s camera
humanoid - sprinting in Roblox

How to allow players to sprint

Sprinting allows a player to gain a short burst of speed for a few seconds. We can display a sprint bar in our game that also slowly recharges or have this hidden just in code. You can also allow players to sprint whenever they hold down the shift button with no limits.
To make this happen we’ll need to use UserInputService in a LocalScript. Add your script under StarterPlayerScripts.

Code Example

local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")

local defaultWalkSpeed = 16
local sprintSpeed = 32

local function getPlayerHumanoid()
	local player = players.LocalPlayer
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")
	return humanoid	
end

local function increasePlayerSpeed()
	local humanoid = getPlayerHumanoid()
	humanoid.WalkSpeed = sprintSpeed
end

local function resetPlayerSpeed()
	local humanoid = getPlayerHumanoid()
	humanoid.WalkSpeed = defaultWalkSpeed
end

local function isShiftHeldDown()
	return userInputService:IsKeyDown(Enum.KeyCode.LeftShift) or userInputService:IsKeyDown(Enum.KeyCode.RightShift)
end

local function onInputBegan(inputObject, processed)
	if isShiftHeldDown() then
		print("sprinting enabled")
		increasePlayerSpeed()
	end
end

local function onInputEnded(inputObject, processed)
	if not isShiftHeldDown() then
		print("sprinting disabled")
		resetPlayerSpeed()
	end
end
userInputService.InputBegan:Connect(onInputBegan)
userInputService.InputEnded:Connect(onInputEnded)

With this script, the player will double its speed while left or right shift keys are pressed.

how to jump in roblox

How to control player’s jumping

We can use the JumpPower property to control how a player jumps. JumpPower works under the gravity set in your game world

Code Example

local players = game:GetService("Players")
 local player = players.LocalPlayer
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
humanoid.JumpPower = 80

We can also set JumpPower to 0 so player’s can no longer jump.

Player accessories

Are you looking to give your player’s sweet accessories?

Within a script, we can use the Accessory class to attach them to a player’s Humanoid. We can use character hats, the classic Roblox accessory, such as the Verified, Bonafide, Plaidified hat as an example.

From it’s catalog url, https://www.roblox.com/catalog/102611803/Verified-Bonafide-Plaidafied, we get its mesh id as 102611803. 

We can then create the mesh using the SpecialMesh class  to modify it’s properties such as its scale and texture.

Next we’ll parent the mesh to a part and create its attachment. The Attachment class defines a constraint on the player’s Humanoid. 

Code Example

Add this under the StarterCharacterScripts folder.

local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")

local playerHat = Instance.new("Accessory")
playerHat.Name = "ClockworksShades"

local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Size = Vector3.new(1, 2, 1)
handle.Parent = playerHat

local headAttachment = Instance.new("Attachment")
headAttachment.Name = "FaceFrontAttachment"
headAttachment.Position = Vector3.new(0, 0, 0.5)
headAttachment.Parent = handle

local mesh = Instance.new("SpecialMesh")
mesh.Name = "Mesh"
mesh.Scale = Vector3.new(1,1.3,1)
mesh.MeshId = "rbxassetid://102611803"
mesh.Parent = handle

humanoid:AddAccessory(playerHat)

You can use the RemoveAccessories function to permanently remove all accessories.

Player emotes

Emotes allow players to express themselves in game by dancing or moving their character to express an emotion. NPCs can also use emotes to add more flair to them.

In our script, we’ll obtain available emotes from the HumanoidDescription property. With this property we can also add new emotes using the SetEmotes function with a Lua table. The emotes Lua table maps string values (emote names) to their resource id.

Once we have emotes ready and available, we simply call PlayEmote on the player’s humanoid property with the emote’s name. 

Code Example

local Players = game:GetService("Players")
local humanoid = Players.LocalPlayer.Character.Humanoid
local humanoidDescription = humanoid.HumanoidDescription

local emoteTable = {
  [“Wave”] = {waveEmoteResourceId},
  [“Cry”] = {cryEmoteResourceId},
  [“Dance”] = {danceEmoteResourceId}
}
humanoidDescription:SetEmotes(emoteTable)

local equippedEmotes = {“Wave”, “Cry”, “Dance”}
humanoidDescription:SetEquippedEmotes(equippedEmotes)

humanoid:PlayEmote(“Wave”)

Control how player’s take damage

The Roblox Humanoid object controls a player’s max health as well as if their character is alive or dead.

Using the HealthDisplayDistance property allows scripts to control how far humanoid health bars display to other players. This is useful when we need to control how far away player’s should see enemy character health levels.

By default, the player’s character dies if a Humanoid’s head part gets detached.

The HealthDisplayType enum allows scripts to control if health is always shown, only damaged, or always off.
The MaxHealth property controls the total amount of health a humanoid has.

Code Example

Let’s create a pad that player’s can walk on to and then add a script to it to change their health. In this script, whenever a player touches the pad, their health goes down by 50.

local killPad = script.Parent

local touched = false
function onPadTouched(otherPart)
	if not touched then
		touched = true
		
		local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
		if humanoid then
			local updatedHealth = humanoid.Health - 50
			humanoid.Health = updatedHealth
			wait(5)
		end

		wait(1)
		touched = false
	end
end
killPad.Touched:Connect(onPadTouched)

The HealthChanged event allows a script to react to health changes. We can use this to control other visual elements or update the health bar.

Player movement events

Players go through several events while moving such as climbing, falling down, free falling, jumping, or running. We can use these events for all kinds of gameplay mechanics.

Here’s an example of how player’s can do a double jump.

We’ll use client LocalScript to define how we want to change player movement behavior.

When a player jumps its humanoid object goes through several states. 

State transitions

  • Jumping state
  • FreeFall state
  • Land state

The script will use the Humanoid:ChangeState function triggered by an input event from UserInputService.

Code Example

wait(0.2)

local UserInputService = game:GetService("UserInputService")
local localPlayer = game.Players.LocalPlayer
local character
local humanoid

local canDoubleJump = false
local hasDoubleJumped = false
local defaultPower
local JUMP_DELAY = 0.2
local DOUBLE_JUMP_POWER = 1.05

function onJumpRequest()
	if not character or not humanoid or not character:IsDescendantOf(workspace) or
		humanoid:GetState() == Enum.HumanoidStateType.Dead then
		return
	end

	if canDoubleJump and not hasDoubleJumped then
		hasDoubleJumped = true
		humanoid.JumpPower = defaultPower * DOUBLE_JUMP_POWER
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
end

local function characterAdded(newCharacter)
	character = newCharacter
	humanoid = newCharacter:WaitForChild("Humanoid")
	hasDoubleJumped = false
	canDoubleJump = false
	defaultPower = humanoid.JumpPower

	humanoid.StateChanged:connect(function(old, new)
		if new == Enum.HumanoidStateType.Landed then
			canDoubleJump = false
			hasDoubleJumped = false
			humanoid.JumpPower = defaultPower
		elseif new == Enum.HumanoidStateType.Freefall then
			wait(JUMP_DELAY)
			canDoubleJump = true
		end
	end)
end

if localPlayer.Character then
	characterAdded(localPlayer.Character)
end

localPlayer.CharacterAdded:connect(characterAdded)
UserInputService.JumpRequest:connect(onJumpRequest)

Notice that once the player is in a Freefall state, the script waits before giving the player the ability to jump again.

roblox player tools

How to use player tools

Tools are items that player’s have on their action bar. These tools may be weapons, items, or actual tools to build something awesome in game. The player’s backpack stores these tools and gets equipped when selected.

Code Example

wait(0.1)

local players = game:GetService("Players")

local function getPlayerHumanoid()
	local player = players.LocalPlayer
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")
	return humanoid	
end

local function makeTool()
	local player = players.LocalPlayer
	local humanoid = getPlayerHumanoid()
	
	local tool = Instance.new("Tool")
	tool.Parent = player:WaitForChild("Backpack")

	local handle = Instance.new("Part")
	handle.Name = "Handle"
	handle.Parent = tool
	handle.BrickColor = BrickColor.Random()

	tool.Enabled = true
	humanoid:EquipTool(tool)

	tool.Activated:Connect(onActivated)
end

local function spawnPart(pos)
	local part = Instance.new("Part")
	part.Anchored = true
	part.Size = Vector3.new(1,1,1)
	part.Position = pos
	part.Parent = game.Workspace
	part.BrickColor = BrickColor.Random()
end

function onActivated()
	local humanoid = getPlayerHumanoid()
	spawnPart(humanoid.TargetPoint)
end

makeTool()

Add this script as a LocalScript under StarterPlayerScripts. As you can see, it creates a tool that allows the player to add a brick to the world. When the tool gets activated it takes the mouse position from TargetPoint and spawns the brick at that position.

Controlling player’s camera

Controlling how far a player can zoom in or out can set the mood for your game. If our player is in a scary situation we can turn it up a notch by limiting their camera movement too. Imagine that player’s can only zoom out just enough to see their character and can only move left or right just a small bit. 

Code Example

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	wait(0.1)
	player.CameraMinZoomDistance = 2
	player.CameraMaxZoomDistance = 20	
end)

Add this script under ServerScriptService and modify CameraMinZoomDistance and CameraMaxZoomDistance to your liking. 

Customizing the camera allows us to create a lot such as cutscenes, third person and first person controls. There’s so much more to discuss about cameras that I’ll go into in another post.

What’s Next

 These 8 essential and helpful tidbits about Roblox Humanoid should help you add more fun to your games. If you haven’t already, I suggest you learn how to teleport players in games or continue your learning journey with how Roblox games work.

For future content updates, please consider joining my email newsletter.

Thank you for reading and as always, stay curious!

Leave a Reply