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)
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)
###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.
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.
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)
lol no, why is it nested in a PlayerAdded event.
The second one is in a server script, server scripts don't have acces to game.Players.LocalPlayer.
server scripts also don't have access to playergui. and there's no reason to put it in a PlayerAdded
Hm, yeah your right. I'll remove the second part.
Now that's just copying green's answer.
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.
1 comment
That's honestly the dumbest problem. But mistakes happen.