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

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

Asked by 6 years ago

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)

0
That's honestly the dumbest problem. But mistakes happen. chexburger 358 — 6y

4 answers

Log in to vote
4
Answered by
green271 635 Moderation Voter
6 years ago
Edited 6 years ago

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
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. lunatic5 409 — 6y
0
Above is also a good solution. green271 635 — 6y
Ad
Log in to vote
0
Answered by
piRadians 297 Moderation Voter
6 years ago

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)

Log in to vote
0
Answered by
jaschutte 324 Moderation Voter
6 years ago
Edited 6 years ago

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)
0
lol no, why is it nested in a PlayerAdded event. User#19524 175 — 6y
0
The second one is in a server script, server scripts don't have acces to game.Players.LocalPlayer. jaschutte 324 — 6y
0
server scripts also don't have access to playergui. and there's no reason to put it in a PlayerAdded green271 635 — 6y
0
Hm, yeah your right. I'll remove the second part. jaschutte 324 — 6y
View all comments (2 more)
0
Now that's just copying green's answer. User#19524 175 — 6y
0
Remove it then? I didn't copy it, he just reacted faster. jaschutte 324 — 6y
Log in to vote
0
Answered by 6 years ago
-- this should work fine
script.Parent.MouseButton1Down:Connect(function(hit)
    if hit.Parent.Humanoid then
        hit.Parent.Humanoid.Health = 0
        wait(5)
    end
end)

Answer this question