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

How do i get a player's character in a local script?

So im trying to get started with some GUI programming by making a simple button that, when you click it, kills you. However, i ran into some issues in finding a player's humanoid. I've tried many methods from this

Error message #1: Players.SRdeagle.PlayerGui.ScreenGui.TextButton.LocalScript:5: attempt to index upvalue 'humanoid' (a nil value)

Attempt #1:

local button = script.Parent
local plrnm = game.Players.LocalPlayer.Name
local humanoid = workspace[plrnm]:FindFirstChild('Humanoid')
local function pressed ()
	humanoid.Health = 0
end
button.MouseButton1Click:Connect(pressed)

to this

Error message #2: Humanoid is not a valid member of Model

Attempt 2#

local button = script.Parent
local plr = game.Players.LocalPlayer
local character = plr.Character
local humanoid = character.Humanoid
local function pressed ()
	humanoid.Health = 0
end
button.MouseButton1Click:Connect(pressed)

Both of these methods i picked up form solved questions on this site with others having the same issue, but the thing is it apparently worked when they did them. I can see that the issue in most of my attempts is that im trying to get a humanoid out of a model, but it makes no sense to me how i can't do that since i can clearly see a humanoid under a 'SRdeagle' (my roblox username) model in workspace when i start the game...

So yeah, if anyone would be kind enough to answer me it would me much appreciated.

P.S. - I have just started with lua programming as you can probably see

4 answers

✓ Accepted answer

It's not that your script hasn't found the players character in the workspace, it's that you're trying to change something on the client which should be changed on the server. So this is what you need to do.

Create a RemoteEvent in ReplicatedStorage and call it "ClickKill"

Now create a server script in ServerScriptService with the following code

game.ReplicatedStorage:FindFirstChild("ClickKill").OnServerEvent:Connect(function(Player)
	local character = plr.Character or workspace[Player.Name]
	character:FindFirstChild("Humanoid").Health = 0
end)

Now you need to change your localscript code to the following

local function pressed()
	game.ReplicatedStorage:FindFirstChild("ClickKill"):FireServer()
end

script.Parent.MouseButton1Click:Connect(pressed)

You do not need the player argument for :FireServer() because it by default includes it as the first argument. Keep note of this.

Look up RemoteEvents and RemoteFunctions for cross client-server communication, you will need this in future.

6 comments

0

Thank you for the long and thorough answer! I will try to do this. Who knew a simple KYS button would be this hard to make

0

You'll get the hang of it, it gets easier after a while. Shoot me a DM if you need any help with anything. MachoPiggies#3484

0

this is so unnecessary

0

Speak to me when you learn to use spacing

1

Thanks for the kindness, i'll hit you up if i need some help.

0

If it worked, feel free to mark my post as correct <3

local script:

script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage["kill player"]:FireServer()
end)

server script:

local remote = Instance.new("RemoteEvent",game.ReplicatedStorage")
remote.Name = "kill player"
remote.OnServerEvent:Connect(function(player)
player.Character.Humanoid.Health = 0
end)

easy enough, when you click it fires a remote event to the server that will kill you

7 comments

0

also make sure the game is set to R15 character or it wont work

0

What would be the advantage in taking health from the server instead of the client?

0

ive just heard from quite a few more experienced scripters that its better to do it on the server instead

0

Way better to do on the server, you'll have to implement server-sided checks if you're doing that.

0

Oh, my bad, didn't realize this was a click to kill GUI XD

0

there is no need to implement some check here since it kills whoever fired the server and its the person who clicked the button that fires it

0

Yeah, I was confused as to why you wanted to do it from the server xD.

I will use Attempt #2. You should define both the character and the humanoid in the function itself, as it appears your script ran before Humanoid was created in your character. Another way to solve this would be to use the WaitForChild() method.

Your code should look something like this(in a LocalScript, located inside the button):

local button = script.Parent
local plr = game.Players.LocalPlayer

local function pressed ()
	local character = plr.Character or plr.CharacterAdded:wait()
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
    	humanoid.Health = 0
	end
end
button.MouseButton1Click:Connect(pressed)

1 comment

0

Thank you! This really works. Honestly ,i'm not sure how that or plr.CharacterAdded:wait() made such a difference but it worked anyways.

local player = game.Players.LocalPlayer player.CharacterAdded:connect(function(character)

end)

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