Humanoid
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Humanoid là một đối tượng đặc biệt cung cấp chức năng của một nhân vật cho các mô hình.Nó cấp cho mô hình khả năng đi bộ vật lý và tương tác với các thành phần khác nhau của trải nghiệm Roblox. Humanoids luôn được cha mẹ bên trong một Model , và mô hình dự kiến sẽ là một tập hợp của BasePart và Motor6D ; phần gốc của tập hợp dự kiến sẽ được đặt tên là HumanoidRootPart .Nó cũng mong đợi một phần có tên là Head được kết nối với phần thân của nhân vật, trực tiếp hoặc gián tiếp.Mặc định, có hai loại nhân vật chính thức được cung cấp bởi Roblox, mỗi loại có bộ quy tắc riêng:
R6
- Một cấu hình nhân vật cơ bản sử dụng 6 phần cho các chi.
- Phần Head phải được gắn vào một phần có tên là Torso , hoặc Humanoid sẽ chết ngay lập tức.
- Các xuất hiện của BodyPart được áp dụng bằng cách sử dụng CharacterMesh đối tượng.
- Một số thuộc tính, chẳng hạn như Humanoid.LeftLeg và Humanoid.RightLeg, chỉ hoạt động với R6.
R15
- Phức tạp hơn R6, nhưng cũng rất linh hoạt và bền vững.
- Sử dụng 15 bộ phận cho các chi.
- Phần Head phải được gắn vào một phần có tên là UpperTorso hoặc Humanoid sẽ chết ngay lập tức.
- Các xuất hiện của BodyPart phải được tổng hợp trực tiếp.
- Có thể được thay đổi kích thước năng động bằng cách sử dụng các đối tượng đặc biệt NumberValue bên trong Humanoid.
- Humanoid sẽ tự động tạo các đối tượng có tên bên trong mỗi chi.
- Nếu một NumberValue được cha trong Humanoid và được đặt tên là một trong những điều theo dõi, nó sẽ được sử dụng để kiểm soát chức năng phóng to:
- Tỉ lệ độ sâu cơ thể
- Tỉ lệ chiều cao cơ thể
- Tỷ lệ chiều rộng cơ thể
- Tỉ lệ đầu
Mẫu mã
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)
Tóm Tắt
Thuộc Tính
Bộ đặt xem liệu nhân vật sẽ tự động nhảy khi chúng đánh vào chướng ngại vật như một người chơi trên thiết bị di động.
AutoRotate bột xác định xem Humanoid có tự động quay theo hướng mà họ đang di chuyển hay không.
Khi được bật, AutomaticScalingEnabled gây thay đổi kích thước của nhân vật theo đáp lời các giá trị trong các giá trị thước con của con người thay đổi.
Xác định xem khớp của con người có bị phá vỡ khi ở trạng thái Enum.HumanoidStateType.Dead hay không.
Một khấu trừ áp dụng cho vị trí chủ đề của Camera khi CameraSubject được đặt thành Humanoid này.
Kiểm soát hành vi khoảng cách của tên và hiển thị sức khỏe của con người nhân tạo.
Đặt văn bản của một Humanoid, hiển thị phía trên đầu họ.
Mô tả Enum.Material mà Humanoid hiện đang đứng trên.Nếu Humanoid không đứng trên bất cứ thứ gì, giá trị của thuộc tính này sẽ là Không khí .
Mô tả sức khỏe hiện tại của Humanoid trong phạm vi [0, Humanoid.MaxHealth].
Sử dụng kết hợp với thuộc tính DisplayDistanceType để kiểm soát khoảng cách từ đó thanh sức khỏe của một hình người có thể được nhìn thấy.
Điều khiển khi thanh sức khỏe của humanoid được phép hiển thị.
Xác định khoảng cách từ mặt đất mà Humanoid.RootPart nên là.
Nếu true , Humanoid nhảy với một lực lên trên.
Cung cấp kiểm soát về độ cao mà Humanoid nhảy lên.
Xác định lượng lực lên bao nhiêu được áp dụng cho Humanoid khi nhảy.
Giá trị tối đa của một humanoid's Health .
Góc độ dốc tối đa mà một humanoid có thể đi bộ mà không bị vấp ngã.
Mô tả hướng mà Humanoid đang đi.
Sử dụng kết hợp với thuộc tính Humanoid.DisplayDistanceType để kiểm soát khoảng cách từ đó tên của một humanoid có thể được nhìn thấy.
Kiểm soát xem tên và thanh sức khỏe của một con người có thể được nhìn thấy phía sau các bức tường hoặc các đối tượng khác hay không.
Xác định xem Humanoid có đang ở trạng thái Enum.HumanoidStateType.PlatformStanding hay không.
Cho phép các nhà phát triển vô hiệu hóa hành vi mà một người chơi Character|character chết nếu Cổ Motor6D bị xóa hoặc thoát kết nối ngay cả trong giây lát.
Mô tả xem liệu điều này Humanoid có sử dụng bộ truyền nhân R6 cũ, hoặc bộ truyền nhân R15 mới.
Một tham chiếu đến đối tượng HumanoidRootPart của con người.
Một tham chiếu đến chỗ ngồi mà Humanoid hiện đang ngồi, nếu có.
Mô tả xem liệu Humanoid có đang ngồi hay không.
Mô tả vị trí 3D mà Player điều khiển Humanoid cuối cùng đã được nhấp vào trong thế giới trong khi sử dụng Tool .
Xác định xem tính năng JumpHeight (giả mạo) hoặc Humanoid.JumpPower (đúng) được sử dụng.
Mô tả tốc độ di chuyển tối đa của hình người trong mỗi giây bằng studs.
Một tham chiếu đến một phần có vị trí đang cố gắng được tiếp cận bởi một con người nhân tạo.
Vị trí mà một humanoid đang cố gắng đạt được, sau khi gọi đến Humanoid:MoveTo() được thực hiện.
Phương Pháp
Gắn Accessory được chỉ định vào cha của hình người.
Tập hợp một cây của Motor6D khớp bằng cách gắn kết các đối tượng Attachment vào nhân vật của một con người nhân tạo.
Bộ Humanoid đặt để nhập vào Enum.HumanoidStateType đã cho.
Trả về một mảng các đối tượng Accessory mà cha của hình nhân đang mặc hiện tại.
Trả lại một bản sao của humanoid's cached HumanoidDescription mô tả ngoạn mục hiện tại của nó.
Chuyển một phần cơ thể sang phương thức này (phần cơ thể phải là em của Humanoid, và là con của một mô hình) để lấy Enum.BodyPartR15 của Part .
Trả về hiện tại của humanoid Enum.HumanoidStateType .
Trả về xem có bật kích hoạt Enum.HumanoidStateType cho Humanoid hay không.
Gây ra Humanoid để đi bộ theo hướng đã cho.
Gây ra Humanoid để cố gắng đi bộ đến vị trí đã cho bằng cách thiết lập Humanoid.WalkToPoint và Humanoid.WalkToPart tính chất.
Loại bỏ tất cả các Accessory đối tượng được mặc bởi cha của hình học con người
Thay thế một phần cơ thể Humanoid bằng một phần khác.
Bộ đặt xem có bật kích hoạt Enum.HumanoidStateType cho Humanoid hay không.
Giảm Humanoid.Health của Humanoid bởi số tiền ** nếu nó không được bảo vệ bởi một ForceField .
- ApplyDescription(humanoidDescription : HumanoidDescription,assetTypeVerification : Enum.AssetTypeVerification):()
Làm cho ngoại hình của nhân vật phù hợp với ngoại hình đã truyền trong HumanoidDescription .
- ApplyDescriptionReset(humanoidDescription : HumanoidDescription,assetTypeVerification : Enum.AssetTypeVerification):()
Làm cho trang phục của nhân vật phù hợp với trang phục đã truyền trong HumanoidDescription , ngay cả sau khi thay đổi bên ngoài.
Chơi cảm xúc và trả lại nếu đã thành công chạy.
Sự Kiện
Bắt lửa khi tốc độ mà một Humanoid đang tăng thay đổi.
Bắt lửa khi Humanoid chết.
Bắt lửa khi Humanoid vào hoặc rời khỏi FallingDown``Enum.HumanoidStateType .
Bắt lửa khi Humanoid vào hoặc rời khỏi Freefall``Enum.HumanoidStateType .
Bắt lửa khi Humanoid vào hoặc rời khỏi GettingUp``Enum.HumanoidStateType .
Bắt lửa khi Humanoid.Health thay đổi (hoặc khi Humanoid.MaxHealth được cài đặt)
Bắt lửa khi Humanoid vào và rời khỏi Jumping``Enum.HumanoidStateType .
Bắt lửa khi Humanoid kết thúc đi bộ đến một mục tiêu được tuyên bố bởi Humanoid:MoveTo() .
Bắt lửa khi Humanoid vào hoặc rời khỏi PlatformStanding``Enum.HumanoidStateType .
Bắt lửa khi Humanoid vào hoặc rời khỏi Ragdoll``Enum.HumanoidStateType .
Bắt lửa khi tốc độ mà Humanoid đang chạy thay đổi.
Bị sa thả khi một Humanoid hoặc Seat hoặc VehicleSeat đang ngồi trong một hoặc đứng dậy.
Bắt lửa khi trạng thái của Humanoid thay đổi.
Bắt lửa khi Humanoid:SetStateEnabled() được gọi trên Humanoid.
Bắt lửa khi Humanoid vào hoặc rời khỏi StrafingNoPhysics``Enum.HumanoidStateType .
Bắt lửa khi tốc độ mà một Humanoid bơi trong Terrain nước thay đổi.
Bắt lửa khi một trong những chi của con người tiếp xúc với một khác BasePart .
Thuộc Tính
AutoJumpEnabled
Mẫu mã
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local button = script.Parent
local function update()
-- Cập nhật văn bản nút
if player.AutoJumpEnabled then
button.Text = "Auto-Jump is ON"
else
button.Text = "Auto-Jump is OFF"
end
-- Phản ánh thuộc tính trong nhân vật của người chơi, nếu họ có một
if player.Character then
local human = player.Character:FindFirstChild("Humanoid")
if human then
human.AutoJumpEnabled = player.AutoJumpEnabled
end
end
end
local function onActivated()
-- Bật/tắt nhảy tự động
player.AutoJumpEnabled = not player.AutoJumpEnabled
-- Cập nhật tất cả những thứ khác
update()
end
button.Activated:Connect(onActivated)
update()
AutoRotate
Mẫu mã
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
BreakJointsOnDeath
CameraOffset
Mẫu mã
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
Mẫu mã
local humanoid = script.Parent
humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Viewer
humanoid.HealthDisplayDistance = 0
humanoid.NameDisplayDistance = 100
DisplayName
EvaluateStateMachine
FloorMaterial
Health
HealthDisplayDistance
HealthDisplayType
HipHeight
Jump
JumpHeight
JumpPower
MaxHealth
MaxSlopeAngle
Mẫu mã
local player = game.Players.LocalPlayer
local char = player.CharacterAdded:wait()
local h = char:FindFirstChild("Humanoid")
h.MaxSlopeAngle = 30
MoveDirection
Mẫu mã
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
NameOcclusion
Mẫu mã
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
RequiresNeck
RigType
RootPart
SeatPart
Sit
TargetPoint
UseJumpPower
WalkSpeed
WalkToPart
WalkToPoint
Mẫu mã
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)
Phương Pháp
AddAccessory
Tham Số
Lợi Nhuận
Mẫu mã
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
Lợi Nhuận
Mẫu mã
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)
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
Tham Số
Lợi Nhuận
Mẫu mã
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)
GetAccessories
Lợi Nhuận
Mẫu mã
local Players = game:GetService("Players")
local function onPlayerAddedAsync(player)
local connection = player.CharacterAppearanceLoaded:Connect(function(character)
-- Tất cả các phụ kiện đã được tải tại thời điểm này
local humanoid = character:FindFirstChildOfClass("Humanoid")
local numAccessories = #humanoid:GetAccessories()
print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
humanoid:RemoveAccessories()
end)
-- Hãy chắc chắn rằng chúng ta cắt kết nối với người chơi sau khi họ rời đi
-- để cho phép người chơi nhận được rác được thu thập
player.AncestryChanged:Wait()
connection:Disconnect()
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)
GetLimb
Tham Số
Lợi Nhuận
Mẫu mã
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
Lợi Nhuận
Mẫu mã
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
Tham Số
Lợi Nhuận
Mẫu mã
local humanoid = script.Parent:WaitForChild("Humanoid")
-- Set state
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
-- Get state
print(humanoid:GetStateEnabled(Enum.HumanoidStateType.Jumping)) -- false
Move
Tham Số
Lợi Nhuận
Mẫu mã
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
Tham Số
Lợi Nhuận
Mẫu mã
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
Lợi Nhuận
Mẫu mã
local Players = game:GetService("Players")
local function onPlayerAddedAsync(player)
local connection = player.CharacterAppearanceLoaded:Connect(function(character)
-- Tất cả các phụ kiện đã được tải tại thời điểm này
local humanoid = character:FindFirstChildOfClass("Humanoid")
local numAccessories = #humanoid:GetAccessories()
print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
humanoid:RemoveAccessories()
end)
-- Hãy chắc chắn rằng chúng ta cắt kết nối với người chơi sau khi họ rời đi
-- để cho phép người chơi nhận được rác được thu thập
player.AncestryChanged:Wait()
connection:Disconnect()
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)
ReplaceBodyPartR15
Tham Số
Lợi Nhuận
SetStateEnabled
Tham Số
Lợi Nhuận
Mẫu mã
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
Tham Số
Lợi Nhuận
Mẫu mã
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
Lợi Nhuận
ApplyDescription
Tham Số
Lợi Nhuận
ApplyDescriptionReset
Tham Số
Lợi Nhuận
Sự Kiện
Climbing
Tham Số
Mẫu mã
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
Mẫu mã
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)
HealthChanged
Tham Số
Mẫu mã
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)
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
MoveToFinished
Tham Số
Mẫu mã
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)
Running
Tham Số
Mẫu mã
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
Tham Số
Mẫu mã
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
Tham Số
Mẫu mã
local character = script.Parent
local primaryPart = character.PrimaryPart
-- tạo hạt
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
-- lắng nghe trạng thái humanoid
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)
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
Tham Số
Mẫu mã
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)
Touched
Tham Số
Mẫu mã
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)