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

how can i lower a players leaderstats when they die?

so i have a leaderstat in my game called "Lives" but i dont know how to lower it by 1 whenever the player dies, i dont know where to start so i made this script which is probably not even close. also the amount of lives the player starts with is 25 if it helps.

local player = game.Players.LocalPlayer
local lives = player.leaderstats.Lives

if player.Humanoid.Health = 0 then
	lives.Value = lives.Value - 1
end

thanks

1 comment

0

Made a mistake to the code, and I've edited it

1 answer

✓ Accepted answer

You can use the Died event that fires everytime the humanoid dies.

(This should be a normal script in ServerScriptService)

game.Players.ChildAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local hum = char:FindFirstChildOfClass('Humanoid')
        hum.Died:Connect(function()
            local lives = player:WaitForChild('leaderstats'):WaitForChild('Lives')
            lives.Value = lives.Value - 1
        end)
    end)
end)

Should be done in a normal script, because changing the lives on the client will not replicate to the server

3 comments

0

it didnt work this is what i got: game.Players.ChildAdded:Connect(function(player) player.CharacterAdded:Connect(function(char) local hum = char:FindFirstChildOfClass('Humanoid') hum.Died:Connect(function() local lives = player:WaitForChild('leaderstats'):WaitForChild('Lives') lives = lives - 1 end) end) end)

0

Edited code above

0

ChildAdded? Why wouldn't you use PlayerAdded. You're accessing Players service.

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