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

Kill script should work fine but for some reason it doesn't work?

This should work I think, but for some reason it doesn't...

player = LocalPlayer

script.Parent.MouseButton1Down:connect(function()
player.Character.Humanoid.Health = 0
wait(5)
end)

1 comment

0

That's honestly the dumbest problem. But mistakes happen.

4 answers

✓ Accepted answer

###Ancestory

LocalPlayer is an object of the Players service. The script is reading it as a variable rather then the player.

The correct way to write it is: game.Players.LocalPlayer.

Updated Code:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:Connect(function()
player.Character.Humanoid.Health = 0
wait(5)
end)

###connect vs Connect

connect is deprecated. You should be using Connect.

###Variables

You should make all your variables local variables by adding local infront of the name. This is good practice and should be done at all times (global variables clutter the enviroment)

#Edit Reason

Changed all the titles into bigger & cleaner text. Answer was not changed in any way.

2 comments

2

Also, for the future, if you wish to kill the player, you can just use player.Character:BreakJoints(). If you want to deal a certain amount of damage on the player, you can use player.Character.Humanoid:TakeDamage(amountofhealth). This way you can shorten your code, even if it's only a little bit. The more compact the better as long as it's just as efficient.

0

Above is also a good solution.

It is because of: player = LocalPlayer Try:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:connect(function()
	player.Character.Humanoid.Health = 0
	wait(5)
end)

6 comments

0

lol no, why is it nested in a PlayerAdded event.

0

The second one is in a server script, server scripts don't have acces to game.Players.LocalPlayer.

0

server scripts also don't have access to playergui. and there's no reason to put it in a PlayerAdded

0

Hm, yeah your right. I'll remove the second part.

0

Now that's just copying green's answer.

0

Remove it then? I didn't copy it, he just reacted faster.

-- this should work fine
script.Parent.MouseButton1Down:Connect(function(hit)
	if hit.Parent.Humanoid then
		hit.Parent.Humanoid.Health = 0
		wait(5)
	end
end)

LocalPlayer by itself is recognized as nil. It should be referenced as a game.Players object.

Try this!

local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Down:Connect(function()
plr.Character.Humanoid.Health = 0
end)

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