Unable to run function multiple times?

Hey developers!

I’m trying to implement a lives system, but the function that takes away a live when the player dies, seems to only run once.

local lives = game:GetService('Workspace').LivesLeft
local restart = game:GetService('ReplicatedStorage').Restart

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.RespawnLocation = game:GetService('Workspace')['1'].Base.Spawn
	
	player.CharacterAdded:Wait()
	player.Character:WaitForChild('Humanoid')
	player.Character.Humanoid.Died:Connect(function()
		print(1)
		if lives.Value == 1 then
			lives.Value -= 1
			restart:FireClient(player)
		else
			lives.Value -= 1
		end
	end)
end)

If anyone would be able to help I would be very thankful.

Have a great day, iamajust

2 Likes

First of all, is this a single player game, if not then, well everyone in the server will loose a live when anyone dies, not only just them. Consider storing the lives value in the player themselves

It’s a single-player game, yes.

You are connecting the Died event to their first character, after they have died they will have a new one. Try this instead:

local lives = game:GetService('Workspace').LivesLeft
local restart = game:GetService('ReplicatedStorage').Restart

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.RespawnLocation = game:GetService('Workspace')['1'].Base.Spawn

	player.CharacterAdded:Connect(function(character)
		character:WaitForChild('Humanoid').Died:Connect(function()
			print(1)
			if lives.Value == 1 then
				lives.Value -= 1
				restart:FireClient(player)
			else
				lives.Value -= 1
			end
		end)
	end)
end)
2 Likes