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

how can i lower a players leaderstats when they die?

Asked by 5 years ago

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

0
Made a mistake to the code, and I've edited it Rare_tendo 3000 — 5y

1 answer

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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

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) poopypigeon245 22 — 5y
0
Edited code above Rare_tendo 3000 — 5y
0
ChildAdded? Why wouldn't you use PlayerAdded. You're accessing Players service. xPolarium 1388 — 5y
Ad

Answer this question