人形是一種特殊對象,可以為模型提供角色的功能。它授予模型能夠實際地走動並與 Roblox 體驗的各種組件互動的能力。人形總是在 Model 內被父系化,模型應該是由 BasePart 和 Motor6D 組成的;裝配的根部分應該被命名為 HumanoidRootPart 。它還期望一個名為 Head 的部分連接到角色的軀體部分,直接或間接地。預設情況下,Roblox 提供兩種官方角色模型,每種都有自己的規則集:
R6
- 一個基本的角色支架,使用 6 個零件來控制肢體。
- Head部分必須附加到名為Torso的部分上,否則機器人將立即死亡。
- 身體部位外觀使用 CharacterMesh 個對象進行應用。
- 某些屬性,例如 Humanoid.LeftLeg 和 Humanoid.RightLeg,只能與 R6 一起使用。
R15
- 比 R6 更複雜,但也更靈活和堅固。
- 使用 15 個零件來支援肢體。
- Head部分必須附加到名為UpperTorso的部分上,否則機器人將立即死亡。
- 身體部位的外觀必須直接組裝。
- 可以使用特殊的 NumberValue 對象在 Humanoid 內部進行動態縮放,以便適應不同的需求。
- 人形將自動在每個肢體內創建Vector3Value名稱為OriginalSize的物體。
- 如果在人形內部有一個 NumberValue 並且命名為以追蹤中之一,它將用於控制縮放功能:
- 身體深度比例
- 身體高度比例
- 身體寬度比例
- 頭部比例
範例程式碼
This LocalScript makes the camera bobble as the player's character walks around, utilizing both the Humanoid's CameraOffset and MoveDirection. It should be parented inside of the StarterCharacterScripts so that it is distributed into a player's character as expected.
local RunService = game:GetService("RunService")
local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")
local function updateBobbleEffect()
local now = tick()
if humanoid.MoveDirection.Magnitude > 0 then -- Is the character walking?
local velocity = humanoid.RootPart.Velocity
local bobble_X = math.cos(now * 9) / 5
local bobble_Y = math.abs(math.sin(now * 12)) / 5
local bobble = Vector3.new(bobble_X, bobble_Y, 0) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, 0.25)
else
-- Scale down the CameraOffset so that it shifts back to its regular position.
humanoid.CameraOffset = humanoid.CameraOffset * 0.75
end
end
RunService.RenderStepped:Connect(updateBobbleEffect)
概要
屬性
設置角色在移動裝置上作為玩家時是否會自動跳躍時遇到障礙物。
自動旋轉設置是否會自動將人形旋轉到面向他們移動的方向。
啟用時,自動縮放啟用會導致人形的子縮放值變更對角色的尺寸產生影響,當人形的子縮放值變更時。
決定是否在 Enum.HumanoidStateType.Dead 狀態下,人形關節會破碎。
在相機的主題位置應用偏移值時,當相機主題設為這個人形時。
控制人形名稱和生命值顯示的距離行為。
設置人形的文字,顯示在他們頭上。
描述 Enum.Material 當前站在的 Humanoid 。如果 Humanoid 沒有站在任何東西上,這個屬性的值將是 空氣 。
描述人形的當前狀態在範圍 [0, ] 上。
與 DisplayDistanceType 屬性一起使用來控制從哪裡可以看到人形條的生命值。
當人形健康條允許顯示時,控制人形的健康條可以顯示。
決定 Humanoid.RootPart 應該離地面的距離。
如果 true ,跳躍 Humanoid 以上升的力量跳躍。
提供控制 Humanoid 跳躍到的高度。
決定在跳躍時對 Humanoid 應用多少向上力。
人形的最大值 Health 。
機器人在沒有滑行的情況下可以走行的最大坡度角度。
描述 Humanoid 正在走的方向。
與 Humanoid.DisplayDistanceType 屬性一起使用來控制從哪裡可以看到人形名稱的距離。
控制是否可以在牆壁或其他物體後面看到機器人的名稱和生命條。
決定是否 Humanoid 目前在 Enum.HumanoidStateType.PlatformStanding 狀態。
讓開發人員停用玩家死亡的行為,如果頸部 被移除或暫時切斷,即使是一瞬間。
描述這個 Humanoid 是否使用舊的 R6 角色骨架,或新的 R15 角色骨架。
一個引用人形的 人形根部件 對物件。
如果有的話,指向 Humanoid 目前坐在的座位的參考。
描述是否 Humanoid 目前坐著。
描述使用 控制世界上最後一次點擊的 3D 位置,而使用一個 。
決定是否使用 JumpHeight (假) 或 Humanoid.JumpPower (真) 屬性。
描述人形的最大移動速度以每秒鐘的點數。
一個指向一個部件的參考,其位置正在被人形機器人嘗試到達。
在呼叫 Humanoid:MoveTo() 之後,機器人嘗試達到的位置。
方法
附加指定的 Accessory 到人形的父元素。
組合一個由 Motor6D 個聯結構成的樹,通過附加人形角色中的 Attachment 個物體來一起附加。
將 Humanoid 設為進入指定的 Enum.HumanoidStateType 。
返回人形的父親目前正在穿戴的一組 Accessory 物件。
返回人形的緩存 HumanoidDescription 副本,描述其目前外觀。
將身體部位傳給此方法(身體部位應該是人形的兄弟,並且是模型的孩子)來獲得 Enum.BodyPartR15 的 Part 。
返回人形的當前 Enum.HumanoidStateType。
返回是否啟用 Enum.HumanoidStateType 對於 Humanoid 。
使 Humanoid 走在指定的方向。
導致 Humanoid 嘗試前往指定位置,通過設置 Humanoid.WalkToPoint 和 Humanoid.WalkToPart 屬性來實現。
移除所有由人形的父元素親穿戴的 Accessory 物件。
動態地替換人形身體部位使用不同的部分。
設置是否啟用給定的 Enum.HumanoidStateType 對 Humanoid 。
將 Humanoid.Health 的 Humanoid 降低到指定的 數量 ,如果未受到 ForceField 的保護。
- ApplyDescription(humanoidDescription : HumanoidDescription,assetTypeVerification : Enum.AssetTypeVerification):()
使角色的外觀與傳入的 HumanoidDescription 相匹配。
- ApplyDescriptionReset(humanoidDescription : HumanoidDescription,assetTypeVerification : Enum.AssetTypeVerification):()
使角色的外觀與傳入的 HumanoidDescription 相匹配,即使在外部變更後。
播放表情並返回,如果成功運行。
活動
當 Humanoid 上升的速度發生變化時,發生火災。
當 Humanoid 死亡時發生火災。
當 Humanoid 進入或離開 FallingDown``Enum.HumanoidStateType 時發生火災。
當 Humanoid 進入或離開 Freefall``Enum.HumanoidStateType 時發生火災。
當 Humanoid 進入或離開 GettingUp``Enum.HumanoidStateType 時發生火災。
當 Humanoid.Health 變更時 (或當 Humanoid.MaxHealth 被設設定時) 發生火災。
當 Humanoid 進入並離開 Jumping``Enum.HumanoidStateType 時發生火災。
當 Humanoid 完成走向由 Humanoid:MoveTo() 宣言的目標時,發生火災。
當 Humanoid 進入或離開 PlatformStanding``Enum.HumanoidStateType 時發生火災。
當 Humanoid 進入或離開 Ragdoll``Enum.HumanoidStateType 時發生火災。
當 Humanoid 運行的速度發生變化時,發生火災。
當 Humanoid 坐在 Seat 或 VehicleSeat 時,發射時間。
當 Humanoid 狀態變更時發生火災。
在 Humanoid:SetStateEnabled() 被呼叫時發生火災,當 Humanoid 被呼叫時。
當 Humanoid 進入或離開 StrafingNoPhysics``Enum.HumanoidStateType 時發生火災。
當人形的肢體與另一個BasePart接觸時發生火災。
屬性
AutoJumpEnabled
「自動跳躍啟用」設定是否會讓 Humanoid 嘗試自動跳過它正在走向的障礙。
目前,此屬性只在下列條件真實時才能運作:
- 人形的角色模型是 Player.Character 的 Player 。
- 問題的玩家正在使用觸摸控制。
當玩家的角色生成時,屬性值與玩家的 Player.AutoJumpEnabled 屬性匹配 - 這在轉而匹配 StarterPlayer.AutoJumpEnabled 屬性。
範例程式碼
This code sample is meant for a TextButton. It allows the player to toggle the auto-jumping behavior while on a mobile device.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local button = script.Parent
local function update()
-- Update button text
if player.AutoJumpEnabled then
button.Text = "Auto-Jump is ON"
else
button.Text = "Auto-Jump is OFF"
end
-- Reflect the property in the player's character, if they have one
if player.Character then
local human = player.Character:FindFirstChild("Humanoid")
if human then
human.AutoJumpEnabled = player.AutoJumpEnabled
end
end
end
local function onActivated()
-- Toggle auto-jump
player.AutoJumpEnabled = not player.AutoJumpEnabled
-- Update everything else
update()
end
button.Activated:Connect(onActivated)
update()
AutoRotate
自動旋轉屬性描述是否人形會自動旋轉到面向他們移動的方向。當設為真實時,角色模型會逐漸轉向面向其移動方向,當人形走動時。當設為 false 時,角色模型將維持在其目前旋轉中,除非對 HumanoidRootPart 應用旋轉力。
如果角色模型是玩家的角色,則人形的旋轉行為會受到使用者遊戲設定的旋轉類型屬性的影響。
當自動旋轉屬性設為真時,旋轉類型屬性對人形的旋轉有以下效果:
<th>行為</th><th>上下文</th></tr></thead><tbody><tr><td>運動相對</td><td /><td /></tr><tr><td>相機相對</td><td>角色將會旋轉以面向相攝影機的方向。</td><td>玩家將相機切換到第一人稱模式,或者他們處於換擋模式。</td></tr></tbody>
旋轉類型 |
---|
範例程式碼
This script adds the functionality of a button to a part, which switches the AutoRotate property of whoever touches it.
local button = script.Parent
local enabled = true
local ON_COLOR = BrickColor.Green()
local OFF_COLOR = BrickColor.Red()
local function touchButton(humanoid)
if enabled then
enabled = false
button.BrickColor = OFF_COLOR
if humanoid.AutoRotate then
print(humanoid:GetFullName() .. " can no longer auto-rotate!")
humanoid.AutoRotate = false
else
print(humanoid:GetFullName() .. " can now auto-rotate!")
humanoid.AutoRotate = true
end
task.wait(1)
button.BrickColor = ON_COLOR
enabled = true
end
end
local function onTouched(hit)
local char = hit:FindFirstAncestorWhichIsA("Model")
if char then
local humanoid = char:FindFirstChildOfClass("Humanoid")
if humanoid then
touchButton(humanoid)
end
end
end
button.Touched:Connect(onTouched)
button.BrickColor = ON_COLOR
AutomaticScalingEnabled
機器人有六個兒童尺寸值,包括BodyDepthScale、BodyHeightScale、BodyProportionScale、BodyTypeScale、BodyWidthScale、HeadScale。變更這些原因之一的值會導致角色的身體部位和配件變更尺寸,但只有當 AutomaticScalingEnabled 是真實的時候才會如此。
CameraOffset
相機偏移量屬性指定相攝影機的主題位置偏移時其 Camera.CameraSubject 設為此 Humanoid 。
抵消在對象空間中,相對於人形的 HumanoidRootPart 的方向。例如,一個偏移值 Vector3 的 (0, 10, 0) 偏移將玩家的相機移到玩家的人形上方 10 個單位。
範例程式碼
This LocalScript makes the camera bobble as the player's character walks around, utilizing both the Humanoid's CameraOffset and MoveDirection. It should be parented inside of the StarterCharacterScripts so that it is distributed into a player's character as expected.
local RunService = game:GetService("RunService")
local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")
local function updateBobbleEffect()
local now = tick()
if humanoid.MoveDirection.Magnitude > 0 then -- Is the character walking?
local velocity = humanoid.RootPart.Velocity
local bobble_X = math.cos(now * 9) / 5
local bobble_Y = math.abs(math.sin(now * 12)) / 5
local bobble = Vector3.new(bobble_X, bobble_Y, 0) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, 0.25)
else
-- Scale down the CameraOffset so that it shifts back to its regular position.
humanoid.CameraOffset = humanoid.CameraOffset * 0.75
end
end
RunService.RenderStepped:Connect(updateBobbleEffect)
DisplayDistanceType
顯示距離類型 屬性控制人形的名稱和生命值顯示的距離行為。這個屬性使用 Enum.HumanoidDisplayDistanceType 枚列設定,有三個可用值,每個都有自己的規則集:
- 當設為 None 時,人形的名稱和生命條在任何情況下都不會出現。
請參閱角色名稱/生命值顯示以獲得關於控制角色名稱和生命值條的深入指南。
範例程式碼
This example demonstrates how to set a Humanoid's Humanoid.DisplayerDistanceType, Humanoid.HealthDisplayDistance, and Humanoid.NameDisplayDistance properties. These properties determine how a humanoid's healthbar and name are rendered for a player.
First, we change the DisplayDistanceType to Viewer using Enum.HumanoidDisplayDistanceType. When set to viewer, the humanoid's Name and healthbar will be displayed based on the distance settings of the humanoid viewing them.
Then, the humanoid's HealthDisplayDistance is set to 0. Setting the property to 0 hides the healthbar completely. It is not displayed at any distance.
Finally, the humanoid's NameDisplayDistance is set to 100. This means that the humanoid's name will be visible to other humanoid's within 100 studs.
This example should work as expected when placed inside a Script that is a child of the humanoid.
local humanoid = script.Parent
humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Viewer
humanoid.HealthDisplayDistance = 0
humanoid.NameDisplayDistance = 100
DisplayName
DisplayName 是一個決定人形在可見時顯示名稱的屬性。預設情況下,新的人形將有空白字串的值。如果 DisplayName 是空字串,機器人的名稱顯示將預設為機器人的父元素名稱屬性。
玩家角色載入
當玩家載入他們的角色時,是否自動或透過使用 LoadCharacter() ,創建引擎的人形將會將其 DisplayName 屬性設為玩家的 DisplayName 屬性。
新手角色和新手人形oid子
當 Humanoid 命名的 StarterHumanoid 被指派到 StarterPlayer 時,或當 Humanoid 存在於名為 StarterCharacter 的模型中時,顯示名稱屬性在玩家在遊戲中載入角色時會被尊重。引擎只會覆蓋人形的 DisplayName 屬性與玩家的 DisplayName 屬性,如果 Humanoid.DisplayName 的 StarterHumanoid 是空字串。
EvaluateStateMachine
FloorMaterial
這是一個只讀屬性,用於描述目前站在Enum.Material上的Humanoid。它可以與兩種常見的 Parts 和 Terrain 體素一起使用。
下面的代碼示範如何使用 Object:GetPropertyChangedSignal() 聆聽此屬性變更時。當人形站在的材料變更時,它會列印一個訊息,指示新材料被站在上面。
local Humanoid = route.to.humanoid
Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
print("New value for FloorMaterial: " .. tostring(Humanoid.FloorMaterial))
end)
警告
- 當 Humanoid 沒有站在地板上時,此特性的值將設為 空氣。
- 這發生因為枚列屬性不能擁有空值。
- 雖然在實務中,零件不應該使用該材料,但如果零件的材料設為空氣,可能會造成一些混亂。
- Humanoid 的角色模型必須能與地板碰撞,否則將無法偵測到。
- 您無法測試 Humanoid 是否使用此屬性游泳。您應該使用其 Humanoid:GetState() 函數。
Health
此屬性代表 Humanoid 的當前狀態。值只能在 0 和 MaxHealth 之間的範圍內使用。如果人形已死亡,此屬性將持續設為 0。
請注意,TakeDamage() 函數可以用來從 Health 減去,而不是直接設置屬性。
生命值重生
預設情況下,被動生命重生腳本會自動插入人形。這會導致非死亡玩家角色每秒重生 1% 的 MaxHealth 。若要停用此重生行為,請將空白的 Script 命名為 生命值 的 StarterCharacterScripts 添加到 。
生命條顯示
當 Health 小於 MaxHealth 時,經驗中會顯示生命條。生命條的顯示行為取決於 HealthDisplayDistance 和 HealthDisplayType 。
請參閱角色名稱/生命值顯示以獲得關於控制角色名稱和生命值條的深入指南。
死亡
當角色的生命值達到 0 時,Humanoid 自動切換到 Enum.HumanoidStateType.Dead 狀態。在這個狀態下,Health 被鎖定為 0;然而,沒有錯誤或警告設置死機器人的 Health 值為正的非零值。
HealthDisplayDistance
此屬性是與 DisplayDistanceType 屬性一起使用的數字,用於控制從哪裡可以看到人形生物的生命條的距離。
請參閱角色名稱/生命值顯示以獲得關於控制角色名稱和生命值條的深入指南。
HealthDisplayType
此屬性控制當人形的生命條可以顯示時。預設情況下,此屬性設為 DisplayWhenDamaged ,使生命條只在人形怪物的 Health 小於其 MaxHealth 時顯示。它也可以設為 AlwaysOn ,使生命條始終顯示,或 AlwaysOff ,使它永遠不會顯示。
請注意,此特性獨立於人形的 HealthDisplayDistance 屬性,負責在特定距離讓健康條消失。如果 Humanoid.HealthDisplayType|HealthDisplayType 設為 AlwaysOn,它仍會依照如何配置 HealthDisplayDistance 而褪色。
請參閱角色名稱/生命值顯示以獲得關於控制角色名稱和生命值條的深入指南。
HipHeight
決定當人形站立時,RootPart 應離地面的距離 (以厘米為單位)。RigType 影響此屬性的行為方式。
對於 R15 裝備,適當的臀部高度已預設,以確保 RootPart 高度正確。不使用腿的高度。人形的總高度可以用下列方程式來描述:
Height = (0.5 * RootPart.Size.Y) + HipHeight
對於 R6 裝備,HipHeight 而不是描述相對偏移值。人形的總高度可以用下列方程式描述:
Height = LeftLeg.Size.Y + (0.5 * RootPart.Size.Y) + HipHeight
Jump
如果 true , Humanoid 跳躍以相等於 Humanoid.JumpPower 或 Humanoid.JumpHeight 高度的向上力跳躍,取決於 Humanoid.UseJumpPower 的值。
JumpHeight
提供對 Humanoid 跳躍高度的控制,以厘米為單位。此屬性的初始值由 StarterPlayer.CharacterJumpHeight 的值決定,其預設值為 7.2。
雖然將此屬性設為 0 將有效防止機器人跳躍,但建議通過停用 狀態來禁用跳躍。
此屬性只能在 屬性 視窗中顯示,如果 Humanoid.UseJumpPower 假 ,否則不相關 (反而使用 Humanoid.JumpPower)。
JumpPower
決定在跳躍時對 Humanoid 應用多少向上力。這個屬性的初始值由 StarterPlayer.CharacterJumpPower 的值決定,默認值為 50,並受到 0 和 1000 之間的限制。請注意,跳躍也會受到Workspace.Gravity屬性的影響,該屬性決定了由重力造成的加速。
雖然將此屬性設為 0 將有效防止機器人跳躍,但建議通過停用 狀態來禁用跳躍。
此屬性只能在 屬性 視窗中顯示,如果 Humanoid.UseJumpPower 真實 ,否則不相關 (而是使用 Humanoid.JumpHeight )。
MaxHealth
人形的最大值 Health 。
這個屬性的值與 Health 屬性一起使用,以縮小預設健康條顯示。當人形的 Health 達到 MaxHealth 時,其生命條可能不會顯示,取決於其 HealthDisplayType 屬性。
MaxSlopeAngle
此屬性決定了人形可以攀爬的最大坡度角度。如果坡道的角度大於人形的 MaxSlopeAngle,它們將滑下坡道。
當角色生成時,此屬性會根據 StarterPlayer.CharacterMaxSlopeAngle 的值設置。
此特性的值被限制在 0° 和 89° 之間。它預設為 89°,因此人形可以預設攀爬任何他們想要的坡度。
範例程式碼
The example below demonstrates the effect of the MaxSlopAngle property by limiting the maximum slope the Players.LocalPlayer can walk up to 30°. The local player will slide down any slope greater than 30°.
This code below works as expected when placed in a LocalScript.
local player = game.Players.LocalPlayer
local char = player.CharacterAdded:wait()
local h = char:FindFirstChild("Humanoid")
h.MaxSlopeAngle = 30
MoveDirection
移動方向 是一個只讀屬性,描述 Humanoid 正在走的方向,作為單位向量或零長度向量力。方向在世界空間中被描述。
因為此屬性是只讀的,因此無法由 Script 或 LocalScript 設置。
範例程式碼
This LocalScript makes the camera bobble as the player's character walks around, utilizing both the Humanoid's CameraOffset and MoveDirection. It should be parented inside of the StarterCharacterScripts so that it is distributed into a player's character as expected.
local RunService = game:GetService("RunService")
local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")
local function updateBobbleEffect()
local now = tick()
if humanoid.MoveDirection.Magnitude > 0 then -- Is the character walking?
local velocity = humanoid.RootPart.Velocity
local bobble_X = math.cos(now * 9) / 5
local bobble_Y = math.abs(math.sin(now * 12)) / 5
local bobble = Vector3.new(bobble_X, bobble_Y, 0) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, 0.25)
else
-- Scale down the CameraOffset so that it shifts back to its regular position.
humanoid.CameraOffset = humanoid.CameraOffset * 0.75
end
end
RunService.RenderStepped:Connect(updateBobbleEffect)
NameDisplayDistance
名稱顯示距離 屬性是用於與Humanoid.DisplayDistanceType一起控制從哪裡可以看到人形名稱的距離的數字。
請參閱角色名稱/生命值顯示以獲得關於控制角色名稱和生命值條的深入指南。
NameOcclusion
控制是否可以在牆壁或其他物體後面看到機器人的名稱和生命條。此屬性是 Enum.NameOcclusion 值,可配置以排除所有名稱、敵人名稱或完全禁用閉包。
在 LocalPlayer 沒有與它相關的 Humanoid 情況下,此屬性會應用於主題 Humanoid 。
請參閱角色名稱/生命值顯示以獲得關於控制角色名稱和生命值條的深入指南。
範例程式碼
In the below example, Player|Players will not be able to see each other's Player.Character names when they are obscured behind BasePart|BaseParts.
local Players = game:GetService("Players")
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.NamOcclusion = Enum.NameOcclusion.OccludeAll
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
PlatformStand
決定是否 Humanoid 目前在 Enum.HumanoidStateType.PlatformStanding 狀態。當真實時,人形會處於自由下降狀態,無法移動工具。此狀態與坐下相似,除了跳躍不會從狀態中釋放人形外。
RigType
RigType描述是否使用舊的 R6 角色骨架或較新的 R15 角色骨架。
R6 裝備使用 6 個可見的 Parts 而 R15 裝備使用 15 個可見的 Parts 。R15 裝備比 R6 裝備有更多關節,使它們在動畫時更具多功能性。
請注意,如果此屬性設置不正確,Humanoid將無法正常運作。例如,如果 R15 人形的 儀式類型 設為 R6,那麼 Humanoid 將死亡,因為沒有被稱為 BasePart 的 軀體 連接到被稱為 BasePart 的 呼叫 。
RootPart
對人形的 HumanoidRootPart 物件的參考,這是控制人形在 3D 世界中移動的根部零件。這部分通常隱形。
對於 R15 角色, Model.PrimaryPart 的 Player.Character 模型設為 HumanoidRootPart 。
對於 R6 角色,Model.PrimaryPart 設為 Head 部分。
SeatPart
座椅零件是指 Humanoid 目前坐在的座椅的參考,如果有。此屬性的值可以是 Seat 或 VehicleSeat 。如果人形目前沒有坐在座位上,將會是 零 。
注意:
- 要判斷 Humanoid 是否當前坐著或不坐著,請參閱 Humanoid.Sit
Sit
Sit 屬性是一個 boolean,表示 Humanoid 是否目前坐著。Humanoids 可以強制設置此屬性值為真值,以進入坐下狀態。如果 Humanoid 在坐下狀態下沒有附在座位上,它的腿上就會絆倒,而不會發生碰撞。A Humanoid 可以通過跳躍從坐下狀態中逃脫。
注意:
- 可以透過連接到 Humanoid.Seated 事件來偵測當人形坐下時。
TargetPoint
不要使用 此特性只與啟用實驗模式的情況下才能運作,已全面停止使用。
此屬性描述了空間中的 3D 位置,在該位置上 Player 控制此 Humanoid 最後點擊使用了 Tool 裝備。
此特性主要由經典工具使用,當他們啟動工具時,判斷人形目標是什麼。如果您給NPC一個經典火箭發射器,設置其 目標點 ,然後呼叫工具的Tool:Activate(),您可以讓NPC在目標點發射火箭。
UseJumpPower
當角色生成時,此屬性會根據 StarterPlayer.CharacterUseJumpPower 的值設置,默認為真值。
當跳躍時,將此設為真值,Humanoid.JumpHeight 值將用於確保機器人跳到那個高度。將此設為 false,Humanoid.JumpPower 值將用於應用向上力。
WalkSpeed
這個屬性描述了 Humanoid 能夠每秒鐘步行多快。它預設為值 StarterPlayer.CharacterWalkSpeed (16),即玩家角色每秒可以在任何方向移動 16 個單位。
注意事項
WalkToPart
WalkToPart 是指人形嘗試到達的零件的參考。當零件傳送為人形的 Humanoid:MoveTo() 函數的第二個參數時,此屬性通常會設置。
當 WalkToPart 設定並啟用人形,並且人形正在努力達到零件時,它會持續更新其 Vector3 目標成為零件位置,加上與旋轉零件相關的對象空間中翻譯的 Humanoid.WalkToPoint 。
這可以在 Luau 中描述為:
goal = humanoid.WalkToPart.CFrame:pointToObjectSpace(humanoid.WalkToPoint)
警告
- 設置 WalkToPart 的值不足以讓人形開始跟隨零件。
- 當人形開始嘗試達到目標時,值班機器人會被提示開始嘗試達到目標,當 WalkToPoint 值變更時。
- 這可能在未來改變。
- 如果機器人沒有達到目標,其 達到目標 狀態將在 8 秒後過期。
- 這是為了防止 NPC 被困在等待 Humanoid.MoveToFinished 發觸發的狀態。
- 如果你不想發生這種情況,你應該反覆呼叫 MoveTo 使時間限制保持重置。
WalkToPoint
走向點描述了人形在空間中嘗試達到的 3D 位置,後來由人形的 Humanoid:MoveTo() 功能提醒他這麼做。
如果人形的 Humanoid.WalkToPart 被設定,目標由將步行點相對於零件位置和旋轉轉換而設置。如果沒有設設定「走向零件」,則人形會嘗試直接到達走向點指定的 3D 位置。
警告
- 要讓機器人開始走向特定點,必須將 WalkToPoint 值變更為不同值,以讓機器人開始走向特定點。
- 如果您想將人形走路到 0,0,0,您應該使用人形的 MoveTo 功能。
- 這可能在未來改變。
- 如果機器人沒有達到目標,其 達到目標 狀態將在 8 秒後過期。
- 這是為了防止 NPC 被困在等待 Humanoid.MoveToFinished 發觸發的狀態。
- 如果你不想發生這種情況,你應該反覆呼叫 MoveTo 使時間限制保持重置。
範例程式碼
This code sample includes a function that avoids the 8 second timeout on Humanoid:MoveTo() by calling Humanoid:MoveTo() again before the timeout elapses. It also includes an optional andThen parameter where developers can pass a function to be called when the humanoid reaches its destination.
local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen(reached)
end
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
task.spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
task.wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
local function andThen(reached)
print((reached and "Destination reached!") or "Failed to reach destination!")
end
moveTo(script.Parent:WaitForChild("Humanoid"), Vector3.new(50, 0, 50), andThen)
方法
AddAccessory
這個方法會將指定的 Accessory 附加到人形的父父元素。
當此方法被呼叫時,會將 Accessory 附加到角色,以搜尋人形的父中包含與飾品的 Attachment 相同名稱的 Attachment 的 處理器 ,以便找到共享相同名稱的 Part 。如果找到一個, 處理 部分將使用Weld與Attachment連接,焊接將配置為Attachments。
如果找不到所需的 Attachment ,則 Accessory 將繼續作為人形的父級,但將被解開。
通常,配件焊接會在伺服器上創建,但在某些情況下可以在客戶端上創建。在這些情況下,客戶端呼叫 AddAccessory() 可能不會總是產生所需的行為,您可以使用 BuildRigFromAttachments() 強制預期的焊接作品。
參數
返回
範例程式碼
This script generates the "Clockwork's Shades" Accessory from scratch, and then attaches it to the player's character using Humanoid.AddAccessory You should paste this code into a regular script, and then parent it inside of the StarterPlayer's StarterCharacterScripts folder.
local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")
local clockworksShades = Instance.new("Accessory")
clockworksShades.Name = "ClockworksShades"
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Size = Vector3.new(1, 1.6, 1)
handle.Parent = clockworksShades
local faceFrontAttachment = Instance.new("Attachment")
faceFrontAttachment.Name = "FaceFrontAttachment"
faceFrontAttachment.Position = Vector3.new(0, -0.24, -0.45)
faceFrontAttachment.Parent = handle
local mesh = Instance.new("SpecialMesh")
mesh.Name = "Mesh"
mesh.Scale = Vector3.new(1, 1.3, 1)
mesh.MeshId = "rbxassetid://1577360"
mesh.TextureId = "rbxassetid://1577349"
mesh.Parent = handle
humanoid:AddAccessory(clockworksShades)
BuildRigFromAttachments
這個方法組裝了一個 Motor6D 節點樹,用於 Humanoid 。 Motor6D 節點是用於播放 Animations 的必要條件。
從人形的 RootPart 開始,這個方法會收集所有在當前部分中使用 Attachments 作為父級的 RigAttachment 名稱。然後搜尋與附件相同名稱的角色中的匹配附件。使用這兩個附件,根據兩個附件和附件的Motor6D的零件,生成一個關節,基於兩個附件和附件的CFrame的零件。
Humanoid:BuildRigFromAttachments() 也會縮放字元並設置身體顏色。
返回
範例程式碼
A Lua port of the Humanoid's BuildRigFromAttachments function, so that the recursive behavior of the function can be seen.
local function createJoint(jointName, att0, att1)
local part0, part1 = att0.Parent, att1.Parent
local newMotor = part1:FindFirstChild(jointName)
if not (newMotor and newMotor:IsA("Motor6D")) then
newMotor = Instance.new("Motor6D")
end
newMotor.Name = jointName
newMotor.Part0 = part0
newMotor.Part1 = part1
newMotor.C0 = att0.CFrame
newMotor.C1 = att1.CFrame
newMotor.Parent = part1
end
local function buildJointsFromAttachments(part, characterParts)
if not part then
return
end
-- first, loop thru all of the part's children to find attachments
for _, attachment in pairs(part:GetChildren()) do
if attachment:IsA("Attachment") then
-- only do joint build from "RigAttachments"
local attachmentName = attachment.Name
local findPos = attachmentName:find("RigAttachment")
if findPos then
-- also don't make double joints (there is the same named
-- rigattachment under two parts)
local jointName = attachmentName:sub(1, findPos - 1)
if not part:FindFirstChild(jointName) then
-- try to find other part with same rig attachment name
for _, characterPart in pairs(characterParts) do
if part ~= characterPart then
local matchingAttachment = characterPart:FindFirstChild(attachmentName)
if matchingAttachment and matchingAttachment:IsA("Attachment") then
createJoint(jointName, attachment, matchingAttachment)
buildJointsFromAttachments(characterPart, characterParts)
break
end
end
end
end
end
end
end
end
local function buildRigFromAttachments(humanoid)
local rootPart = humanoid.RootPart
assert(rootPart, "Humanoid has no HumanoidRootPart.")
local characterParts = {}
for _, descendant in ipairs(humanoid.Parent:GetDescendants()) do
if descendant:IsA("BasePart") then
table.insert(characterParts, descendant)
end
end
buildJointsFromAttachments(rootPart, characterParts)
end
local humanoid = script.Parent:WaitForChild("Humanoid")
buildRigFromAttachments(humanoid)
A script that generates an R15 character from scratch using a package's assetId.
local AssetService = game:GetService("AssetService")
local InsertService = game:GetService("InsertService")
local MarketplaceService = game:GetService("MarketplaceService")
local PACKAGE_ASSET_ID = 193700907 -- Circuit Breaker
local function addAttachment(part, name, position, orientation)
local attachment = Instance.new("Attachment")
attachment.Name = name
attachment.Parent = part
if position then
attachment.Position = position
end
if orientation then
attachment.Orientation = orientation
end
return attachment
end
local function createBaseCharacter()
local character = Instance.new("Model")
local humanoid = Instance.new("Humanoid")
humanoid.Parent = character
local rootPart = Instance.new("Part")
rootPart.Name = "HumanoidRootPart"
rootPart.Size = Vector3.new(2, 2, 1)
rootPart.Transparency = 1
rootPart.Parent = character
addAttachment(rootPart, "RootRigAttachment")
local head = Instance.new("Part")
head.Name = "Head"
head.Size = Vector3.new(2, 1, 1)
head.Parent = character
local headMesh = Instance.new("SpecialMesh")
headMesh.Scale = Vector3.new(1.25, 1.25, 1.25)
headMesh.MeshType = Enum.MeshType.Head
headMesh.Parent = head
local face = Instance.new("Decal")
face.Name = "face"
face.Texture = "rbxasset://textures/face.png"
face.Parent = head
addAttachment(head, "FaceCenterAttachment")
addAttachment(head, "FaceFrontAttachment", Vector3.new(0, 0, -0.6))
addAttachment(head, "HairAttachment", Vector3.new(0, 0.6, 0))
addAttachment(head, "HatAttachment", Vector3.new(0, 0.6, 0))
addAttachment(head, "NeckRigAttachment", Vector3.new(0, -0.5, 0))
return character, humanoid
end
local function createR15Package(packageAssetId)
local packageAssetInfo = MarketplaceService:GetProductInfo(packageAssetId)
local character, humanoid = createBaseCharacter()
character.Name = packageAssetInfo.Name
local assetIds = AssetService:GetAssetIdsForPackage(packageAssetId)
for _, assetId in pairs(assetIds) do
local limb = InsertService:LoadAsset(assetId)
local r15 = limb:FindFirstChild("R15")
if r15 then
for _, part in pairs(r15:GetChildren()) do
part.Parent = character
end
else
for _, child in pairs(limb:GetChildren()) do
child.Parent = character
end
end
end
humanoid:BuildRigFromAttachments()
return character
end
local r15Package = createR15Package(PACKAGE_ASSET_ID)
r15Package.Parent = workspace
ChangeState
這個方法使 Humanoid 進入指定的 Enum.HumanoidStateType ,描述 Humanoid 目前正在進行的活動。
請查看 Enum.HumanoidStateType 頁面,了解特定狀態的更多信息,因為有些狀態的名稱不直觀。例如,Enum.HumanoidStateType.Running 描述人形腿在地面上的狀態,包括靜止時。
由於 Humanoid 的預設行為,一些狀態在設設定時會自動變更。例如:
- 當機器人不在水中時,設置狀態為 Enum.HumanoidStateType.Swimming 將導致它自動設置為 Enum.HumanoidStateType.GettingUp 。
- 因為未使用,將狀態設為 Enum.HumanoidStateType.PlatformStanding 將導致機器人狀態自動設為 Enum.HumanoidStateType.Running 。
請注意,若要使用此方法設置 Humanoid 狀態,您必須從 LocalScript 進行,並且客戶端必須擁有 網路所有權 的 Player.Character 。或者,您可以從服務器端呼叫此方法 Script ,但服務器必須擁有玩家角色的網絡所有權。
也可以參考 Humanoid:SetStateEnabled() 啟用或禁用特定狀態,以及 Humanoid:GetState() 獲得當前的人形狀態。
參數
執行 Enum.HumanoidStateType 的那個 Humanoid 是要執行的。
返回
範例程式碼
This code, when placed inside a LocalScript in StarterPlayer.StarterCharacterScripts, will allow the player's character to perform a double jump.
local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local doubleJumpEnabled = false
humanoid.StateChanged:Connect(function(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not doubleJumpEnabled then
task.wait(0.2)
if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
doubleJumpEnabled = true
end
end
elseif newState == Enum.HumanoidStateType.Landed then
doubleJumpEnabled = false
end
end)
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.Space then
if doubleJumpEnabled then
if humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
task.spawn(function()
doubleJumpEnabled = false
end)
end
end
end
end)
EquipTool
以下示例將導致 Player 裝備名為 Workspace 的工具 ** .
local Players = game:GetService("Players")local Workspace = game:GetService("Workspace")local player = Players:FindFirstChildOfClass("Player")if player and player.Character thenlocal humanoid = player.Character:FindFirstChildWhichIsA("Humanoid")if humanoid thenlocal tool = Workspace:FindFirstChild("Tool")if tool thenhumanoid:EquipTool(tool)endendend
當此方法被呼叫時,Humanoid 將首先自動卸下目前已裝備的所有Tools。
雖然它們會被裝備,Tools 對於Tool.RequiresHandle 沒有握手把的情況下,true 將無法運作,無論這種方法是用來裝備它們還是不是。
也見:
- 要卸下工具,請使用 Humanoid:UnequipTools()
參數
返回
GetAccessories
這個方法返回人形的父親目前正在穿戴的一個 array 的 Accessory 對象。所有這樣的 Accessory 對象將包括在內,無論是否附加。
如果 Humanoid 沒有 Accessory 對象,將返回一個空陣列。
也可以參考 Humanoid:AddAccessory() 將 Accessory 附加到人形的父元素。
返回
範例程式碼
This code sample will wait for accessories to fully load, print out how many there are, and then destroy them all.
local Players = game:GetService("Players")
local function onPlayerAddedAsync(player)
local connection = player.CharacterAppearanceLoaded:Connect(function(character)
-- All accessories have loaded at this point
local humanoid = character:FindFirstChildOfClass("Humanoid")
local numAccessories = #humanoid:GetAccessories()
print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
humanoid:RemoveAccessories()
end)
-- Make sure we disconnect our connection to the player after they leave
-- to allow the player to get garbage collected
player.AncestryChanged:Wait()
connection:Disconnect()
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)
GetAppliedDescription
此方法返回人形的緩存 HumanoidDescription 的副本,描述其目前的外觀。這可以用來快速決定角色的外觀,並使用 Humanoid:ApplyDescription() 方法將外觀分配給其他角色。
也見「也見」
- Players:GetHumanoidDescriptionFromUserId() 返回描述傳入用戶的虛擬人偶的 HumanoidDescription 。
- Players:GetHumanoidDescriptionFromOutfitId() 返回一個 HumanoidDescription 其參數被初始化匹配傳入服務器端服裝素材的參數。
- Player:LoadCharacterWithHumanoidDescription() 會生成一名玩家,外觀來自傳入的 HumanoidDescription 。
返回
GetBodyPartR15
這個方法返回什麼是 Enum.BodyPartR15 一個 Part 或 Enum.BodyPartR15.Unknown 如果零件不是 R15 身體部分。這個方法讓開發人員可以獨立於實際身體部位名稱來恢復玩家身體部位,而不是返回枚列。
可與 Humanoid:ReplaceBodyPartR15() 一起使用。例如,如果玩家的身體部位觸碰到某物,這個功能會返回一個零件實個體、實例。開發者可以查找身體的哪一部分,例如頭部或手臂。然後根據那部分是什麼,開發人員可以執行一些遊戲行動或將那部分替換為其他部分-也許顯示傷害。
這個方法可能對那些需要命中位置的遊戲有用。例如,它可以用來確定玩家是否在腿部受傷,然後根據傷害緩慢他們的速度。
參數
指定的零件正在被檢查,以確認是否為 R15 身體零件。
返回
指定零件的 R15 身體部分類型或未知,如果零件不是身體部分。
GetLimb
這個方法返回與給定 Part 相關的 Enum.Limb。它適用於 R15 和 R6 儀器,例如:
-- 對於 R15print(humanoid:GetLimb(character.LeftUpperLeg)) -- Enum.Limb.左腿print(humanoid:GetLimb(character.LeftLowerLeg)) -- Enum.Limb.左腿print(humanoid:GetLimb(character.LeftFoot)) -- Enum.Limb.左腿-- 對於 R6print(humanoid:GetLimb(character:FindFirstChild("Left Leg"))) -- Enum.Limb.LeftLeg
請注意,如果零件的父親未設為人形的父父元素,則 Humanoid:GetLimb() 將會發出錯誤。
參數
返回
範例程式碼
Put this in a LocalScript. The output will vary based on if the humanoid is R6 or R15.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
for _, child in pairs(character:GetChildren()) do
local limb = humanoid:GetLimb(child)
if limb ~= Enum.Limb.Unknown then
print(child.Name .. " is part of limb " .. limb.Name)
end
end
GetState
這個方法返回人形的當前 Enum.HumanoidStateType , 描述 Humanoid 目前正在進行的活動, 例如跳或游泳。
也可以參考 Humanoid:SetStateEnabled() 啟用或禁用特定狀態,以及 Humanoid:ChangeState() 來變更目前的人形狀態。
返回
範例程式碼
This code, when placed inside a LocalScript in StarterPlayer.StarterCharacterScripts, will allow the player's character to perform a double jump.
local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local doubleJumpEnabled = false
humanoid.StateChanged:Connect(function(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not doubleJumpEnabled then
task.wait(0.2)
if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
doubleJumpEnabled = true
end
end
elseif newState == Enum.HumanoidStateType.Landed then
doubleJumpEnabled = false
end
end)
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.Space then
if doubleJumpEnabled then
if humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
task.spawn(function()
doubleJumpEnabled = false
end)
end
end
end
end)
GetStateEnabled
GetStateEnabled 方法返回是否啟用 Enum.HumanoidStateType 對於 Humanoid 。
人形狀態描述人形目前正在進行的活動。
當特定的 Enum.HumanoidStateType 被禁用時,人形永遠無法進入那個狀態。無論使用 Humanoid:ChangeState() 或 Roblox 內部人形代碼來變更狀態,這都是真實的。
也見:
- 對於啟用或禁用人形狀態時發生的事件,請參閱 Humanoid.StateEnabledChanged
- 要啟用或禁用 Humanoid 狀態使用 Humanoid:SetStateEnabled()
參數
提供的 Enum.HumanoidStateType 。
返回
是否啟用指定的 Enum.HumanoidStateType 。
範例程式碼
The code below sets the value of the humanoid jumping state to false using Humanoid:SetStateEnabled() and then retrieves and prints the value of this state (false) using Humanoid:GetStateEnabled().
local humanoid = script.Parent:WaitForChild("Humanoid")
-- Set state
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
-- Get state
print(humanoid:GetStateEnabled(Enum.HumanoidStateType.Jumping)) -- false
Move
這個方法使 Humanoid 走在指定的 Vector3 方向。
默認情況下,方向是以世界單條款來表示的,但如果 relativeToCamera 參數是 true,方向就相對於 CFrame 的 CurrentCamera 。由於在 Roblox 中負向 Z 方向被認為是「向前」,以下代碼將使人形走在 CurrentCamera 的方向。
humanoid:Move(Vector3.new(0, 0, -1), true)
當此方法被呼叫時,Humanoid將移動直到方法再次呼叫。然而,此方法在下一個框架中會被 Roblox 的預設角色控制腳指令碼覆蓋。這可以通過每個框架呼叫此函數的 RunService:BindToRenderStep() (見例子) 或覆蓋控制腳本在 StarterPlayerScripts 來避免。
此方法可以在伺服器上呼叫,但只有當伺服器擁有 人形機器人的裝配所有權 時才應該這麼做。
也可以參考 Humanoid:MoveTo() 將 Humanoid 步行到一個點,以及 Player:Move() 有效呼叫此功能。
參數
要走的方向。
設為 true 如果 moveDirection 參數應該與 CurrentCamera 相對。
返回
範例程式碼
This code sample uses the Humanoid:Move() function to make the player's Character walk in the direction of the Camera. RunService:BindToRenderStep() is required here as the default control scripts will overwrite the player's movement every frame.
To run this sample, place it inside a LocalScript parented to StarterCharacterScripts.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
RunService:BindToRenderStep("move", Enum.RenderPriority.Character.Value + 1, function()
if player.Character then
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
humanoid:Move(Vector3.new(0, 0, -1), true)
end
end
end)
MoveTo
這個方法使 Humanoid 嘗試到指定位置走路,通過設置 Humanoid.WalkToPoint 和 Humanoid.WalkToPart 屬性來實現。
位置 和 零件 參數與 Humanoid.WalkToPoint 和 Humanoid.WalkToPart 將設置的內容相對應。
如果指定 部分 參數,Humanoid 仍會嘗試前往該點。然而,如果零件移動,那麼指向 的點將移動到與零件相同的位置 。如果 部分 參數未指定,則走向 Humanoid 的位置不會變更。
如果機器人沒有達到目標,其 達到目標 狀態將在 8 秒後過期。這樣做是為了讓 NPC 不會被困在等待 Humanoid.MoveToFinished 發觸發的狀態。如果你不想發生這種情況,你應該反覆呼叫 MoveTo 使時間限制保持重置。
MoveTo() 結束,如果下列任何一個條件適用:
角色抵達目的地。
角色被困住,八秒計時器到期。
值 Humanoid.WalkToPoint 或 Humanoid.WalkToPart 的值會改變。
一個腳本呼叫 Humanoid:Move() 使用新的 moveDirection 參數。
參數
設置 Humanoid.WalkToPoint 的位置。
設置 BasePart 以設置 Humanoid.WalkToPart。
返回
範例程式碼
This code sample includes a function that avoids the 8 second timeout on Humanoid:MoveTo() by calling Humanoid:MoveTo() again before the timeout elapses. It also includes an optional andThen parameter where developers can pass a function to be called when the humanoid reaches its destination.
local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen(reached)
end
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
task.spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
task.wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
local function andThen(reached)
print((reached and "Destination reached!") or "Failed to reach destination!")
end
moveTo(script.Parent:WaitForChild("Humanoid"), Vector3.new(50, 0, 50), andThen)
RemoveAccessories
這個方法移除所有由人形的父父元素穿戴的 Accessory 物體。對於玩家 Characters ,這將移除所有帽子和其他配件。
這個方法通過呼叫 Accessory 來移除對象 Instance:Destroy() ,意味著配件的 Parent 被設置為 nil 並被鎖定。
也可以參考 來附加 ,以及 來獲得所有屬於 的對象。
返回
範例程式碼
This code sample will wait for accessories to fully load, print out how many there are, and then destroy them all.
local Players = game:GetService("Players")
local function onPlayerAddedAsync(player)
local connection = player.CharacterAppearanceLoaded:Connect(function(character)
-- All accessories have loaded at this point
local humanoid = character:FindFirstChildOfClass("Humanoid")
local numAccessories = #humanoid:GetAccessories()
print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
humanoid:RemoveAccessories()
end)
-- Make sure we disconnect our connection to the player after they leave
-- to allow the player to get garbage collected
player.AncestryChanged:Wait()
connection:Disconnect()
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)
ReplaceBodyPartR15
動態地在人形中替換 R15/Rthro 肢體部分以不同的部分。部分會自動按照正常尺寸縮放。
這個方法在遊戲期間修改角色或從基礎骨架中建立角色時有用。相關方法 GetBodyPartR15 可以在使用此方法時很有用。
傳入的零件名稱應與傳入的 BodyPartR15 Enum 名稱匹配。
參數
要替換的身體部位。Enum.BodyPartR15.Unknown將失敗。
返回
SetStateEnabled
此方法設置是否啟用給定的 Enum.HumanoidStateType 對 Humanoid 。當特定 Enum.HumanoidStateType 被禁用時,Humanoid 將永遠無法進入該狀態。無論使用 Humanoid:ChangeState() 或 Roblox 內部 Humanoid 代碼進行狀態變更嘗試,都是如此。
請注意,在服務器上使用 SetStateEnabled() 不會將變更複製給客戶端,也不會相反。
參數
需要啟用或禁用的 Enum.HumanoidStateType 。
true 如果 state 要啟用,false 如果 state 要禁用。
返回
範例程式碼
The following sample will require a one second cooldown after a Humanoid has landed before it is able to jump again.
To try this sample, place it inside a LocalScript in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.
local character = script.Parent
local JUMP_DEBOUNCE = 1
local humanoid = character:WaitForChild("Humanoid")
local isJumping = false
humanoid.StateChanged:Connect(function(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not isJumping then
isJumping = true
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
end
elseif newState == Enum.HumanoidStateType.Landed then
if isJumping then
isJumping = false
task.wait(JUMP_DEBOUNCE)
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
end
end
end)
TakeDamage
此方法降低 Humanoid.Health 的 Humanoid 由指定的 數量 如果未受到 ForceField 保護
這個方法接受負值的 數量 參數。這將增加人形的 Humanoid.Health 。但是這只會在沒有 ForceField 存在時產生效果。
力場如何防止受到傷害
A Humanoid 被視為受到 ForceField 保護,如果 A ForceField 符合以下任一條件:
- 被指派給祖先的子孫,除了 以外
無論是否存在任何 現有的傷害,都必須直接設置 。
要了解更多關於如何 ForceFields 保護 Humanoids 的信息,請參閱 ForceField 頁。
參數
傷害或從 Humanoid.Health 中扣除的數量。
返回
範例程式碼
This code, put in a LocalScript, would make the local player take 99 damage only if a ForceField wasn't present.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid:TakeDamage(99)
UnequipTools
未裝備的 將被指派給與 相關的 的父親。
如果沒有配備 Tool,這個方法將無所作為。
雖然 Tools 可以由 NPC (非玩家角色) 裝備,但此方法只能在 Humanoids 上使用相應的 Player 。這是因為需要一個 Backpack 對象來親和未裝備的 Tool 。
也見:
- 要取而代之裝備 Tool ,請使用 Humanoid:EquipTool()
返回
範例程式碼
The following sample will bind the U key to unequip any Tool|Tools the Player currently has equipped.
To use this sample, place it inside a LocalScript within StarterPlayerScripts|StarterPlayer.StarterPlayerScripts.
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local player = Players.LocalPlayer
ContextActionService:BindAction("unequipTools", function(_, userInputState)
if userInputState == Enum.UserInputState.Begin then
if player.Character then
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:UnequipTools()
end
end
end
end, false, Enum.KeyCode.U)
ApplyDescription
這種放棄方法使字元的外觀與傳入的 HumanoidDescription 相匹配。通過的 HumanoidDescription 的副本被儲存為 HumanoidDescription 的 Humanoid 。
這個方法通過假設只有這個方法用於變更角色的外觀,並且在呼叫之間通過其他手段沒有進行任何變更來最佳化。如果在呼叫期間對角色進行更改,則此方法可能不會使角色準確地反映傳入的 HumanoidDescription 。如果您想使用此方法與其他更新角色的方法結合,Humanoid:ApplyDescriptionReset()將始終確保角色反映傳入的HumanoidDescription。
也見「也見」
- Humanoid:GetAppliedDescription() 會返回目前對人形應用的 HumanoidDescription 。
- Players:GetHumanoidDescriptionFromUserId() 返回描述傳入用戶的虛擬人偶的 HumanoidDescription 。
- Players:GetHumanoidDescriptionFromOutfitId() 返回一個 HumanoidDescription 其參數被初始化匹配傳入服務器端服裝素材的參數。
- Player:LoadCharacterWithHumanoidDescription() 會生成一名玩家,外觀來自傳入的 HumanoidDescription 。
參數
您想將角色設為匹相符的 HumanoidDescription 實例。
返回
ApplyDescriptionReset
這種放棄方法使角色的外觀與傳入的 HumanoidDescription 相匹配,即使在外部變更後。通過的 HumanoidDescription 的副本被儲存為 HumanoidDescription 的 Humanoid 。
這個方法會始終確保字元反映了傳入的 HumanoidDescription ,即使在不使用 HumanoidDescription 或 ApplyDescription() 系統的情況下也是如此(例如不使用 ApplyDescriptionReset() 或 )。這與 ApplyDescription() 最佳化的情況相反,可能會不正確地應用 HumanoidDescription 如果角色通過非 HumanoidDescription 系統已被更改。
參數
您想將角色設為匹相符的 HumanoidDescription 實例。
返回
活動
Climbing
當 Humanoid 上升的速度發生變化時,發生火災。
Humanoids 可以爬上由 Parts 或 TrussParts 製成的梯子。
Humanoids 在他們的 Humanoid.WalkSpeed 的 70% 攀爬。
當 Humanoid 停止攀爬時,這個事件並不總是以速度為 0 發射。
也見:
- 對於游泳和跑步,請參閱 Humanoid.Swimming 和 Humanoid.Running 事件
- 您也可以使用 Humanoid 事件偵測當 Humanoid.StateChanged 正在攀登時
- 您可以使用 Humanoid:SetStateEnabled() 函數禁用攀爬
參數
範例程式碼
local Players = game:GetService("Players")
local function onCharacterClimbing(character, speed)
print(character.Name, "is climbing at a speed of", speed, "studs / second.")
end
local function onCharacterAdded(character)
character.Humanoid.Climbing:Connect(function(speed)
onCharacterClimbing(character, speed)
end)
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Died
此事件會在 Humanoid 死亡時發生,通常在 Humanoid.Health 達到 0 時發生。這可能是因為從他們的頭部中斷連線,或直接設置健康屬性造成的。
此事件只會發生,如果 Humanoid 是 Workspace 的子孫。如果 Dead Enum.HumanoidStateType 被禁用,它將不會觸發生。
範例程式碼
The code below would print the player's name, followed by "has died!", whenever a player dies. For example, if the player was named "Shedletsky", "Shedletsky has died!" would be printed to the output when they died.
local Players = game:GetService("Players")
local function onPlayerAdded(player)
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local function onDied()
print(player.Name, "has died!")
end
humanoid.Died:Connect(onDied)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
FallingDown
掉落事件發生時,Humanoid 進入並離開FallingDown``Enum.HumanoidStateType 。
Humanoid將在GettingUp狀態啟用後的3秒鐘進入FallingDown狀態。當發生這種情況時,此事件將發射以 活動 值 為 false ,而 Humanoid.GettingUp 將發射以 活動 值 true 。
參數
FreeFalling
當 Humanoid 進入或離開 Freefall``Enum.HumanoidStateType 時,此事件會發生。
活動參數 代表 是否進入或離開 狀態。
雖然 狀態一般會在 到達地面時結束,但此事件可能會在 有效 與 假 相等時發生,如果狀態在 下降時被更改。因此,您應該使用 Humanoid.StateChanged 並聆聽 Landed 狀態在 Humanoid 降落時工作。
參數
GettingUp
當 Humanoid 進入或離開 Enum.HumanoidStateType.GettingUp 狀態時,這個事件會發生,即在 Humanoid 進入 FallingDown (3 秒) 或 Ragdoll (1 秒) 狀態不久後啟用的轉換狀態。
當 Humanoid 嘗試重新上線時,此事件會先發射 active 參數的 true 以很快再發射 active 參數的 false 。
若要強制 Humanoid 倒下,請使用 Humanoid:ChangeState() 函數與 Enum.HumanoidStateType.FallingDown 。
參數
HealthChanged
此事件會在 Humanoid.Health 變更時發生。然而,如果生命值從等於或大於 Humanoid.MaxHealth 的值增加,它不會發射。
當 Humanoid.Health 達到零時,Humanoid 將死亡,Humanoid.Died 事件將觸發。這個事件將以零值發射。
參數
新值 Humanoid.Health 。
範例程式碼
The following example determines the change in health, printing it to the output. It will only work in a LocalScript.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local currentHealth = humanoid.Health
local function onHealthChanged(health)
local change = math.abs(currentHealth - health)
print("The humanoid's health", (currentHealth > health and "decreased by" or "increased by"), change)
currentHealth = health
end
humanoid.HealthChanged:Connect(onHealthChanged)
end
player.CharacterAdded:Connect(onCharacterAdded)
This code sample allows you to create a simple color-changing health bar using two nested Frames. Paste this into a LocalScript on the inner frame.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Paste script into a LocalScript that is
-- parented to a Frame within a Frame
local frame = script.Parent
local container = frame.Parent
container.BackgroundColor3 = Color3.new(0, 0, 0) -- black
-- This function is called when the humanoid's health changes
local function onHealthChanged()
local human = player.Character.Humanoid
local percent = human.Health / human.MaxHealth
-- Change the size of the inner bar
frame.Size = UDim2.new(percent, 0, 1, 0)
-- Change the color of the health bar
if percent < 0.1 then
frame.BackgroundColor3 = Color3.new(1, 0, 0) -- black
elseif percent < 0.4 then
frame.BackgroundColor3 = Color3.new(1, 1, 0) -- yellow
else
frame.BackgroundColor3 = Color3.new(0, 1, 0) -- green
end
end
-- This function runs is called the player spawns in
local function onCharacterAdded(character)
local human = character:WaitForChild("Humanoid")
-- Pattern: update once now, then any time the health changes
human.HealthChanged:Connect(onHealthChanged)
onHealthChanged()
end
-- Connect our spawn listener; call it if already spawned
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
end
Jumping
當 Humanoid 進入並離開 Jumping``Enum.HumanoidStateType 時,此事件會發生。
當 Humanoid 跳躍時,此事件會在短時間後再次發射,參數為 active 的 true ,之後再次發射參數為 active 的 false 。這次第二次發射與一次 Humanoid 降落不相對應;要達到這一點,請聆聽 Landed Enum.HumanoidStateType 使用 Humanoid.StateChanged 。
您可以使用 Humanoid:SetStateEnabled() 函數來禁用跳躍。
參數
MoveToFinished
當 Humanoid 完成走向由 Humanoid.WalkToPoint 和 Humanoid.WalkToPart 屬性所宣言的目標時,此事件會發生。
Humanoid.WalkToPoint 和 Humanoid.WalkToPart 屬性可以單獨設置,或使用 Humanoid:MoveTo() 函數。
如果 Humanoid 在 8 秒內達到目標,此事件將返回 已達到 作為真實。如果目標在 8 秒內未達到,Humanoid將停止行走,已達到將為假。此時間限制可以通過在時間限制期間再次呼叫 Humanoid:MoveTo() 來重置。
參數
範例程式碼
This code sample includes a function that avoids the 8 second timeout on Humanoid:MoveTo() by calling Humanoid:MoveTo() again before the timeout elapses. It also includes an optional andThen parameter where developers can pass a function to be called when the humanoid reaches its destination.
local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen(reached)
end
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
task.spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
task.wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
local function andThen(reached)
print((reached and "Destination reached!") or "Failed to reach destination!")
end
moveTo(script.Parent:WaitForChild("Humanoid"), Vector3.new(50, 0, 50), andThen)
PlatformStanding
當 Humanoid 進入或離開 PlatformStanding``Enum.HumanoidStateType 時,此事件會發生。
當 Humanoid 處於 PlatformStanding 狀態時,Humanoid.PlatformStand 屬性將為 真實 。
雖然 Humanoid.PlatformStand 設為 真實 ,但 Humanoid 將無法移動工具。有關更多信息,請參閱 Humanoid.PlatformStand 頁面。
平台站 Enum.HumanoidStateType 與現在已停用的 Platform 部分相關。不過,開發人員仍然可以使用它。
參數
Ragdoll
當 Humanoid 進入或離開 Ragdoll``Enum.HumanoidStateType 時,此事件會發生。
active 參數將有值 true 或 false 來表示進入或離開。
使用 Humanoid:SetStateEnabled() 來禁用「獲得」狀態,保持在「布偶」狀態中。
也見:
參數
Running
當 Humanoid 運行的速度發生變化時,此事件會發生。
在執行 Humanoids 封面時,平均每秒鐘的 Humanoid.WalkSpeed 在厘米上。
當 Humanoid 停止執行此事件時,速度為 0 的速度將發射。
也見:
- 對於游泳和攀登,請參閱 Humanoid.Swimming 和 Humanoid.Climbing 事件
- 您也可以使用 Humanoid 事件偵測當 Humanoid.StateChanged 執行時
參數
範例程式碼
Demonstrates connecting to the Humanoid.Running event. The event is connected to every player's humanoid that joins.
The function connected will print whether or not the humanoid is running based on the speed.
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local function onRunning(speed: number)
if speed > 0 then
print(`{localPlayer.Name} is running`)
else
print(`{localPlayer.Name} has stopped`)
end
end
humanoid.Running:Connect(function(speed: number)
onRunning(speed)
end)
Seated
此事件會在 Humanoid 坐在或從 Seat 或 VehicleSeat 起來時發生。
當角色與座位接觸時,它們會附到座位上,並播放坐姿動畫。有關此詳情,請參閱Seat。
- 如果角色坐下,active參數將是 真實 ,而currentSeatPart將是他們目前坐在的座位。
- 如果角色從座位上起來了,active將是 假 ,而currentSeatPart將是nil。
也見:
- Humanoid.Sit , 表示是否有人形目前坐著
- Humanoid.SeatPart , 表示人形目前坐在的座位,如果有。
參數
範例程式碼
This code sample demonstrates when the local player's Character sits down or stands up. It should be placed inside a LocalScript within StarterCharacterScripts in order to run when the player's character spawns in.
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local function onSeated(isSeated, seat)
if isSeated then
print("I'm now sitting on: " .. seat.Name .. "!")
else
print("I'm not sitting on anything")
end
end
humanoid.Seated:Connect(onSeated)
StateChanged
此事件會在 Humanoid 狀態發生變更時發射。
因為沒有「閒置」的人形狀態,你應該使用 Humanoid.Running 事件或聆聽 RootPart 部分的 Velocity 來確定當 Humanoid 停止運動時。
也見「也見」
- Humanoid:GetState() 和 Humanoid:ChangeState() 來獲得和設置狀態。
- Humanoid:SetStateEnabled() 啟用和禁用特定狀態。
參數
人形的前狀態輸入。
人形的當前狀態輸入。
範例程式碼
Emits particles from the local player's Player.Character when they jump. To try this code sample, place it inside a LocalScript parented to StarterCharacterScripts.
local character = script.Parent
local primaryPart = character.PrimaryPart
-- create particles
local particles = Instance.new("ParticleEmitter")
particles.Size = NumberSequence.new(1)
particles.Transparency = NumberSequence.new(0, 1)
particles.Acceleration = Vector3.new(0, -10, 0)
particles.Lifetime = NumberRange.new(1)
particles.Rate = 20
particles.EmissionDirection = Enum.NormalId.Back
particles.Enabled = false
particles.Parent = primaryPart
local humanoid = character:WaitForChild("Humanoid")
local isJumping = false
-- listen to humanoid state
local function onStateChanged(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not isJumping then
isJumping = true
particles.Enabled = true
end
elseif newState == Enum.HumanoidStateType.Landed then
if isJumping then
isJumping = false
particles.Enabled = false
end
end
end
humanoid.StateChanged:Connect(onStateChanged)
The following sample will require a one second cooldown after a Humanoid has landed before it is able to jump again.
To try this sample, place it inside a LocalScript in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.
local character = script.Parent
local JUMP_DEBOUNCE = 1
local humanoid = character:WaitForChild("Humanoid")
local isJumping = false
humanoid.StateChanged:Connect(function(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not isJumping then
isJumping = true
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
end
elseif newState == Enum.HumanoidStateType.Landed then
if isJumping then
isJumping = false
task.wait(JUMP_DEBOUNCE)
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
end
end
end)
StateEnabledChanged
狀態啟用變更事件會在 Humanoid:SetStateEnabled() 在 Humanoid 上被呼叫時發生。
參數包括問題中的 Enum.HumanoidStateType 以及指示是否啟用此狀態的 bool。
也見:
- 要找出狀態是否已啟用,請使用 Humanoid:GetStateEnabled()
- 要聆聽 Humanoid 狀態變更使用 Humanoid.StateChanged
參數
啟用狀態已變更的 Enum.HumanoidStateType 。
如狀態現在已啟用,則為真。
範例程式碼
When a humanoid state changes for the Players.LocalPlayer, the code below prints whether the state has been enabled or disabled.
This code should work as expected when placed in a LocalScript.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local function onStateEnabledChanged(state, enabled)
if enabled then
print(state.Name .. " has been enabled")
else
print(state.Name .. " has been disabled")
end
end
humanoid.StateEnabledChanged:Connect(onStateEnabledChanged)
Strafing
此事件不會在 Humanoid 掃射時發射,並不應該由開發人員使用
此事件會在 Humanoid 進入或離開 StrafingNoPhysics``Enum.HumanoidStateType 時發生。
當 Humanoid 進入 StrafingNoPhysics 狀態時,此事件將以 有效 參數 true 發射。事件將再次發射,使用 活動 等於 假 當 Humanoid 離開 StrafingNoPhysics 狀態時。
這個事件與 StrafingNoPhysics Humanoid 狀態相關,並且在 不 發射時,當 Humanoid 移動平行於面向的方向時,它不會發射。此狀態目前未使用,如果使用 Humanoid:ChangeState() 設置狀態,狀態將恢復到 RunningNoPhysics 。
參數
Swimming
當 Humanoid 在 Terrain 水中游泳的速度發生變化時,此事件會發生。
Humanoids 在 87.5% 的Humanoid.WalkSpeed中游泳。
當 Humanoid 停止游泳時,這個事件不一定會以速度為 0 發射。
也見:
- 對於跑步和攀登,請參閱 Humanoid.Running 和 Humanoid.Climbing 事件
- 您也可以使用 Humanoid 事件偵測當 Humanoid.StateChanged 游泳時
- 您可以使用 Humanoid:SetStateEnabled() 函數禁用游泳
參數
Touched
當人形的肢體與另一個BasePart接觸時,此事件發生。被肢體觸碰的 BasePart ,以及肢體本身,都會被提供。
當屬於 Humanoid 的肢體接觸到自己時,此事件不會發生。
替代品
雖然 Humanoid.Touched 事件很有用,但你應該考慮是否有更適合你需求的替代方案。
- 在大多數情況下,建議連接BasePart.Touched事件,而不是連接BaseParts事件,因為Humanoid.Touched事件會一直發射,當人形移動時。例如,在拋球遊戲中,連接Touched事件來控制球會比使用Humanoid.Touched更實用。
注意事項
- 連接到此事件會導致每個肢體都會創建一個 TouchTransmitter 。
- 目前沒有與 BasePart.TouchEnded 相等的 Humanoids 。
參數
範例程式碼
When placed inside a Player.Character model this code will give a player the 'Midas touch'. Everything their character touches will change to gold.
When the Humanoid dies, this change is undone and the golden BasePart|BaseParts are returned to their original state.
To test this out, place this code inside a Script and place it in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local partInfo = {}
local debounce = false
local function onHumanoidTouched(hit, _limb)
if debounce then
return
end
if not hit.CanCollide or hit.Transparency ~= 0 then
return
end
if not partInfo[hit] then
partInfo[hit] = {
BrickColor = hit.BrickColor,
Material = hit.Material,
}
hit.BrickColor = BrickColor.new("Gold")
hit.Material = Enum.Material.Ice
debounce = true
task.wait(0.2)
debounce = false
end
end
local touchedConnection = humanoid.Touched:Connect(onHumanoidTouched)
local function onHumanoidDied()
if touchedConnection then
touchedConnection:Disconnect()
end
-- undo all of the gold
for part, info in pairs(partInfo) do
if part and part.Parent then
part.BrickColor = info.BrickColor
part.Material = info.Material
end
end
end
humanoid.Died:Connect(onHumanoidDied)