#Lives Stats

5 messages · Page 1 of 1 (latest)

surreal tulip
#

-- Function that handles player death and removes one life from the Lives leaderstat
local function onPlayerDead(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local lives = leaderstats:FindFirstChild("Lives")
if lives and lives.Value > 0 then
lives.Value = lives.Value - 1
if lives.Value <= 0 then
player:Kick("You have run out of lives.")
end
end
end
end

-- Function that connects the onPlayerDead function to player deaths
local function onPlayerEntered(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
onPlayerDead(player)
end)
end)
end

game.Players.PlayerAdded:Connect(onPlayerEntered)

this was supposed to make me lose 1 life when i die ut i lost 2

lilac thistle
#

That works fine. The reason it might be removing 2 lives is because you might have connected 2 of those onPlayerDead events.

grave epoch
#

So that would mean

local Connection
…
player.CharacterAdded….
if Connection then Connection:Disconnect end
…
Connection = humanoid.died…

surreal tulip