Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can i get the player, player character, humanoid and humanoid root part?

Asked by
seikkatsu 110
4 years ago

I find it pretty difficult and confusing to get the player, char, hum , or hum root part so could someone help me with that? I don't have a specific script to show but i'm confused.

0
i wish i could upvote bruce_matthew 4 — 3y

2 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

If you are on a local script:

player = game.Players.LocalPlayer
character = game.Players.LocalPlayer.Character
humanoid = character:WaitForChild("Humanoid")
humanoidRootPart = character:WaitForChild("HumanoidRootPart")

If you're in a normal script, then it depends. Some ROBLOX events such as the event below give you the player, but you can't get the local player because there is no such thing in a normal script.

workspace.Part.Touched:Connect(function(hit)

    if(hit.Parent:FindFirstChild("Humanoid"))then
        local character = hit.Parent
        humanoid = character.Humanoid
        humanoidRootPart = character.HumanoidRootPart
    end

end)

tell me what you want to do if on the server and i can help you

0
thank you! both of your comments helped me out a lot! seikkatsu 110 — 4y
Ad
Log in to vote
0
Answered by
TopBagon 109
4 years ago

Here's what you can use in the local script

local player = game.Players.LocalPlayer -- Player
repeat wait() until player.Character -- You can't use "WaitForChild" because character is not a member of player (player is in players tab while character is in the workspace) so you just gotta wait before the character of that local player appears
local character = player.Character -- Now you can define a variable of character
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart") -- HumanoidRootPart is a member of character which means you can use "WaitForChild" (which will wait until the child in the brackets is loaded) to access it easier
local Humanoid = character:WaitForChild("Humanoid") -- same goes here, humanoid is a member of character so WaitForChild should do the job (just gotta change the word inside the brackets since we're waiting for humanoid

Tho you can't use it in workspace and some other tabs, but there you can use a normal script with this code inside:

game.Players.PlayerAdded:Connect(function(player)
    -- Now we got the player. this is the same as local player
    player.CharacterAdded:Connect(function(character)
    --Waits until the character of the player is added, now you can define any child of character:
        local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
        local Humanoid = character:WaitForChild("Humanoid")
    end)
end)

Hope this helped.

0
thank you! both of your comments helped me out a lot! seikkatsu 110 — 4y

Answer this question