Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I get the players humanoid?

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

✓ Accepted answer

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)

1 comment

0

You might as well just use the Character property

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.

2 comments

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.