Player Character "Dying" when already dead

So I have a death system in my game which enables a death screen on the client and removes one of the player’s “lives” on the server.
Death is registered by using the .Died event on the humanoid of the player on the client. Oddly, I find myself dying multiple times when already dead (so the .Died event seems to fire again for some reason).

humanoid.Died:Connect(module.Toggle)

The local script responsible for handling this is placed inside a ScreenGui which has reset on spawn turned on.

Does anyone know why?

can you actually show us the full code

local died = false

function module.Toggle()
	local hum = player.Character:FindFirstChild("Humanoid")
	if not humanoid then return end
	
	if humanoid.Health <= 0 and died == true then return end
	died = true
	
	mainGUI.Enabled = false
	
	for i, v in LivesFrame:GetChildren() do
		if not v:IsA("UIGridLayout") then
			v:Destroy()
		end
	end
	deathFrame.Visible = true

	deathEvent:FireServer(player)
	
	if lives.Value-1 < 1 then
		messageText.Text = "You ran out of lives! You can't place, upgrade or modify towers anymore. But enemies will still target you. If all players run out of lives, you lose!"		
	else
		if math.random(1,1000) ~= 1 then 
			messageText.Text = module.Messages[math.random(1, #module.Messages)]
			messageText.TextColor = BrickColor.new("Really black")
		else
			messageText.Text = "This is a 1/1000 message!!! (gambling yay)"
			messageText.TextColor = BrickColor.new("New Yeller")
		end
	end
	
	if gameRunning.Value == true then
		if lives.Value-1 > 1 then
			livesText.Text = `~ {lives.Value-1} Lives left ~`
		elseif lives.Value-1 == 1 then
			livesText.Text = "~ 1 Live left ~"
		else
			livesText.Text = "~ 0 Lives left ~"
		end
	else
		livesText.Text = `~ {lives.Value} Lives left ~`
		messageText.Text = "No lives have been removed as the game is not running."
	end
	
	if lives.Value-1 >= 1 then
		for i = 1, lives.Value-1, 1 do
			local newLive = livePrevap:Clone()
			newLive.Visible = true
			newLive.Parent = LivesFrame
		end
	end
	
	local mapData = require(workspace:FindFirstChild("GameEssentials"):FindFirstChild("Map"):FindFirstChild("MapData"))
	local percentage = (lives.Value-1) / mapData.Lives[#players:GetPlayers()] or 1
	fader.BackgroundTransparency = percentage
	
	for i = game:GetService("Players").RespawnTime, 1, -1 do
		respawnText.Text = i
		script.Timer:Play()
		task.wait(1)
	end
	
	died = false
end

Sorry for not showing this earlier

I’m pretty sure there is more to the code than this. Also, you defined hum but started using humanoid for the entire script which I find odd.

No that and the connection is literally all the code (I think I forgot to change the humanoid to the hum variable)

1 Like

^ What does this do?
Nevermind about the second part, lol. Thought it was a child.

It removes the live of the player via the server.

Checking if there already is a death screen (if it’s already visible) could be a fix. You should still check if humanoid.Died:Connect(module.Toggle) is being ran multiple times.

local died = false

function module.Toggle()
	local hum = player.Character:FindFirstChild("Humanoid")
	if not humanoid then return end
	
	if humanoid.Health <= 0 and died == true then return end
    if deathFrame.Visible then return end
	died = true
	
	mainGUI.Enabled = false
	
	for i, v in LivesFrame:GetChildren() do
		if not v:IsA("UIGridLayout") then
			v:Destroy()
		end
	end

	deathFrame.Visible = true

	deathEvent:FireServer(player)
	
	if lives.Value-1 < 1 then
		messageText.Text = "You ran out of lives! You can't place, upgrade or modify towers anymore. But enemies will still target you. If all players run out of lives, you lose!"		
	else
		if math.random(1,1000) ~= 1 then 
			messageText.Text = module.Messages[math.random(1, #module.Messages)]
			messageText.TextColor = BrickColor.new("Really black")
		else
			messageText.Text = "This is a 1/1000 message!!! (gambling yay)"
			messageText.TextColor = BrickColor.new("New Yeller")
		end
	end
	
	if gameRunning.Value == true then
		if lives.Value-1 > 1 then
			livesText.Text = `~ {lives.Value-1} Lives left ~`
		elseif lives.Value-1 == 1 then
			livesText.Text = "~ 1 Live left ~"
		else
			livesText.Text = "~ 0 Lives left ~"
		end
	else
		livesText.Text = `~ {lives.Value} Lives left ~`
		messageText.Text = "No lives have been removed as the game is not running."
	end
	
	if lives.Value-1 >= 1 then
		for i = 1, lives.Value-1, 1 do
			local newLive = livePrevap:Clone()
			newLive.Visible = true
			newLive.Parent = LivesFrame
		end
	end
	
	local mapData = require(workspace:FindFirstChild("GameEssentials"):FindFirstChild("Map"):FindFirstChild("MapData"))
	local percentage = (lives.Value-1) / mapData.Lives[#players:GetPlayers()] or 1
	fader.BackgroundTransparency = percentage
	
	for i = game:GetService("Players").RespawnTime, 1, -1 do
		respawnText.Text = i
		script.Timer:Play()
		task.wait(1)
	end
	
	died = false
end

That’s a good idea thank you. I’m going to try this.

1 Like