Humanoid

Humanoid 方法和属性

TakeDamage减少 Humanoid 的健康值。
MoveTo使 Humanoid 走到指定的位置或部分。
GetState返回 Humanoid 的当前状态,类型为 Enum.HumanoidStateType。
ChangeState强制 Humanoid 切换到指定状态。
RemoveAccessories移除 Humanoid 的父模型所穿戴的所有配件。
ReplaceBodyPartR15替换 R15 身体部件为其他部件。
SetStateEnabled启用或禁用 Humanoid 的指定状态。
UnequipTools卸下 Humanoid 当前装备的工具。
ApplyDescription将 HumanoidDescription 应用于 Humanoid。
ApplyDescriptionReset将 HumanoidDescription 应用于 Humanoid 并重置所有更改。
PlayEmote如果有效,播放指定的表情动作。
Move使 Humanoid 朝指定方向移动。
GetMoveVelocity返回 Humanoid 当前的移动速度。
EquipTool将指定的工具装备给 Humanoid。
Health表示 Humanoid 的当前健康值。
MaxHealth表示 Humanoid 可以拥有的最大健康值。
WalkSpeed决定 Humanoid 行走的速度。
JumpPower控制 Humanoid 跳跃的力量。
HipHeight表示 Humanoid 的臀部离地面的高度。
AutoRotate决定 Humanoid 是否自动旋转面对移动方向。
BreakJointsOnDeath控制 Humanoid 死亡时是否断开关节。
CameraOffset相对于 Humanoid 的摄像机偏移。
FloorMaterial指示 Humanoid 所站地面的材质类型。
NameDisplayDistance显示 Humanoid 名称的距离。
NameOcclusion决定名称标签的显示方式。
Sit指示 Humanoid 是否正在坐下。
TargetPointHumanoid 正在移动的目标点。

Humanoid 健康示例

local Players = game:GetService("Players")

local function onPlayerAdded(player)
	-- Wait for the player's character to load
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")

	-- Example: Setting Health
	print("Current Health:", humanoid.Health)
	humanoid.Health = 50 -- Set health to 50
	print("New Health:", humanoid.Health)


	-- Example: Adding Health
	print("Current Health:", humanoid.Health)
	local healthToAdd = 20
	humanoid.Health = math.min(humanoid.Health + healthToAdd, humanoid.MaxHealth) 
	print("Health after adding health:", humanoid.Health)

	-- Example: Checking if Humanoid is Dead
	humanoid.Died:Connect(function()
		print("Humanoid has died!")
	end)

	-- Example: Changing MaxHealth
	print("Current MaxHealth:", humanoid.MaxHealth)
	humanoid.MaxHealth = 200 -- Set MaxHealth to 200
	print("New MaxHealth:", humanoid.MaxHealth)

	-- Example: Setting Health to MaxHealth
	humanoid.Health = humanoid.MaxHealth
	print("Health set to MaxHealth:", humanoid.Health)
	
	-- Example: Taking Damage
	print("Current Health:", humanoid.Health)
	humanoid.Health = humanoid.Health - 300 -- Directly reduce health by 300
	print("Health after taking damage:", humanoid.Health)

end

-- Connect the function to the PlayerAdded event
Players.PlayerAdded:Connect(onPlayerAdded)
Image 1
Roblox Studio

Humanoid 动画示例

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

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

-- Animation setup
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://131036267483527" -- Replace with your animation asset ID

-- Load the animation onto the humanoid
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack.Looped = false -- Ensure the animation does not loop by default

-- Function to play the animation
local function playAnimation()
	print("playAnimation")
	animationTrack:Play()
end

-- Function to stop the animation
local function stopAnimation()
	print("stopAnimation")
	animationTrack:Stop()
end

-- Function to loop the animation
local function loopAnimation(loopCount)
	animationTrack.Looped = true
	animationTrack:Play()
	print("loopAnimation")

	-- Stop looping after the specified count
	delay(loopCount * animationTrack.Length, function()
		animationTrack.Looped = false
		stopAnimation()
		print("loopAnimation stopped")
	end)
end

-- Detect when "P", "T", or "L" is pressed and trigger the appropriate function
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.P and not gameProcessedEvent then
		print("pressed P")
		playAnimation()
	end
	if input.KeyCode == Enum.KeyCode.T and not gameProcessedEvent then
		print("pressed T")
		stopAnimation()
	end
	if input.KeyCode == Enum.KeyCode.L and not gameProcessedEvent then
		print("pressed L")
		loopAnimation(3)
	end
end)
Image 1
Roblox Studio

Humanoid 移动示例

-- Function to make the humanoid walk to a specific point
local function walkToPoint(point)
    humanoid:MoveTo(point)
end

-- Function to make the humanoid move in a specific direction
local function moveInDirection(direction)
    humanoid:Move(direction)
end

-- Function to stop the humanoid's movement
local function stopMovement()
    humanoid:Move(Vector3.new(0, 0, 0))
end

-- Example usage
local targetPoint = Vector3.new(10, 0, 10) -- Define a target point
walkToPoint(targetPoint) -- Make the humanoid walk to the target point

-- After a delay, make the humanoid move in a specific direction
wait(2)
local direction = Vector3.new(1, 0, 0) -- Define a direction
moveInDirection(direction)

-- After a delay, stop the humanoid's movement
wait(2)
stopMovement()
Image 1
Roblox Studio
  • 使用 Humanoid:MoveTo 时,Humanoid 会自动播放适当的行走或跑步动画。
  • 此方法将 Humanoid 的内部状态设置为行走或跑步,一旦达到目标或移动中断,它会恢复为待机状态。
  • 当 Humanoid 到达目的地或在特定超时时间内未能到达时,会触发 MoveToFinished 事件。

Humanoid 事件示例

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

-- Function to handle when the humanoid finishes moving to a point
humanoid.MoveToFinished:Connect(function(reached)
    if reached then
        print("Humanoid reached the destination")
    else
        print("Humanoid did not reach the destination")
    end
end)

-- Example function stubs for each event
local function onDied()
    print("Humanoid died")
end

local function onRunning(speed)
    print("Humanoid is running at speed:", speed)
end

local function onJumping()
    print("Humanoid is jumping")
end

local function onClimbing()
    print("Humanoid is climbing")
end

local function onGettingUp()
    print("Humanoid is getting up")
end

local function onFreeFalling()
    print("Humanoid is in free fall")
end

local function onFallingDown()
    print("Humanoid is falling down")
end

local function onSeated()
    print("Humanoid is seated")
end

local function onPlatformStanding()
    print("Humanoid is platform standing")
end

local function onSwimming()
    print("Humanoid is swimming")
end

-- Connect the functions to the respective humanoid events
humanoid.Died:Connect(onDied)
humanoid.Running:Connect(onRunning)
humanoid.Jumping:Connect(onJumping)
humanoid.Climbing:Connect(onClimbing)
humanoid.GettingUp:Connect(onGettingUp)
humanoid.FreeFalling:Connect(onFreeFalling)
humanoid.FallingDown:Connect(onFallingDown)
humanoid.Seated:Connect(onSeated)
humanoid.PlatformStanding:Connect(onPlatformStanding)
humanoid.Swimming:Connect(onSwimming)
Image 1
Roblox Studio