Health decrease script

I need a script that decreases the players Health by a few points every second. I found a script in Toolbox that says:

function onPlayerEntered (newPlayer)
while true do
wait(10)
newPlayer.Character.Humanoid.Health=newPlayer.Character.Humanoid.Health-7
end
end

game.Players.ChildAdded:connect (onPlayerEntered)

This script works about half the time, but if I change the numbers, it gives me an error in Output that says ‘attempt to index nil with Humanoid’

Thanks

I would suggest doing
newPlayer.Character.Humanoid:TakeDamage(insertdamageamount)
rather than subtracting health

2 Likes

This would be the better way of going about it, I agree.

@Lostscrews13 I suggest you format your code with your posts, it makes them cleaner and more readable.

1 Like
local function DecreaseHP(Character)
    while true do
        wait(10)
        if Character.Humanoid.Health > 0 then
            Character.Humanoid:TakeDamage(7)
        end
    end
end

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(DecreaseHP)
end)

This should work, idk why you’re not using a CharacterAdded Event it’s easier to use

1 Like

Confused why you have to utilize a function, just simplfy the script

game.Players.PlayerAdded:Connect(function(plr)
    player.CharacterAdded:Connect(function(char)
        while true do
            wait(10)
            char.Humanoid.Health -= 7
        end
    end)
end)

Or if you really want to, then here

local function takeHealth(char)
    while true do
        wait(10)
        char.Humanoid:TakeDamage(7)
    end
end

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        takeHealth(char)
    end)
end)
4 Likes