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)
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
is deprecated. You should be using Connect
.
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)
Changed all the titles into bigger & cleaner text. Answer was not changed in any way.
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)
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)
-- this should work fine script.Parent.MouseButton1Down:Connect(function(hit) if hit.Parent.Humanoid then hit.Parent.Humanoid.Health = 0 wait(5) end end)