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

How can I get the players humanoid?

Asked by 7 years ago

I know there's a way with Touched, but how about when you click a GUI?

my attempt:

local plr = game.Players.LocalPlayer -- i know a humanoid isn't in the player from the playergui


game.Workspace.Tutorial.Section1.Screen.CoverScreen.StartFrame.StartGui.
Next.MouseButton1Click:connect(function() --THIS IS APART OF LINE ATOP, BUT NO ROOM
        game.Workspace.Tutorial.StarterSpawn.Enabled = false
        game.Workspace.Level1.Spawn2.Enabled = true
        plr.Humanoid.Health = 0
    end)

2 answers

Log in to vote
0
Answered by 7 years ago

All you need to do is get the name of the LocalPlayer and you got it.

Add a .Name to your first line of code. local plr = game.Players.LocalPlayer.Name

Then you would just have to edit line 8 of your code. game.Workspace:FindFirstChild(plr)

0
You might as well just use the Character property Perci1 4988 — 7y
Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
7 years ago

Every player has a Character property (equal to their character model). Since you already have the local player, you can use that property.

local chr = plr.Character
local hum = chr.Humanoid

But in the case that either the character or the humanoid is not loaded yet, this would error. So you should definitely wait for them both to exist.

repeat wait() until plr.Character
local chr = plr.Character
local hum = chr:WaitForChild("Humanoid")

If you need more explanation, just comment.

0
Would I do hum.Health = 0? Because I see it waits until the plr, then the characters get the player, and the humanoid gets inside the character and waits for the humanoid DeveloperSolo 370 — 7y
0
The player always exists, so no need to wait for that. In my code, line 1 waits until the character exists. Then it stores the character as a variable. Then line 3 waits until the humanoid exists. You can now do whatever you want to the humanoid using the variable we set for it, so yes, setting its health to 0 is one option. Perci1 4988 — 7y

Answer this question