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