Cant get script to keep searching if a player has died

Hello, I have been trying to do this for 3 days now looking at others questions to see if I can piece something together what I am trying to do with this script is have a Lives counter for an entire team right now I’m just scripting the counter where it goes down when a player on that team dies

I have it where it detects if the player has died for the first time after firing the Event but it doesn’t keep searching it goes down from 5 to 4 but doesn’t keep dropping if i die again

I tried a Repeat loop but I think I did it wrong crashing my studio

local script

local Replicated = game.ReplicatedStorage
local player = game.Players.LocalPlayer
local SLife = Replicated.Stats.SLivesLeft
local ZLife = Replicated.Stats.ZLivesLeft

Replicated.Events.InGameStart.OnClientEvent:Connect(function() -- this works dont worry about this
	wait(2)
	player.Character.Humanoid.WalkSpeed = 16
		
end)
Replicated.Events.InGameStart.OnClientEvent:Connect(function() -- when the Game Starts i want it to check for a players Death
	wait(2)
	player.Character.Humanoid.Died:Connect(function()	
		print("death found")		
		
		if player.Team == game.Teams.Survivor then						
			
			Replicated.Events.LivesChange:FireServer(SLife)				
			
		elseif player.Team == game.Teams.Zombie then
						
			Replicated.Events.LivesChange:FireServer(ZLife)		
						
		end		
	end)
end)

Server Script which works the counter goes down when the value is changed

local Replicated = game:GetService("ReplicatedStorage")

Replicated.Events.LivesChange.OnServerEvent:Connect(function(plr, teamValue)
	teamValue.Value = teamValue.Value - 1
end)

I know it’s simple but I’m somewhat new to scripting and have never used it.Died so I don’t know how to use it

I believe you can’t use .Died on a local script only on a server script. So I would use remote events to get this working.

I know that. Local Script is how I am checking, the issue isn’t that it doesn’t work it’s that it only checks the once and it’ll work then if i die again it wont it doesn’t keep searching after the first death for some reason

Oh my bad I misunderstood sorry. Where is this located by the way?

no worries - this is inside a UI when it detects a players death it fires a Event with a script inside serverscriptservice

Can you add a print event in the server script please. So I can know if it’s locally or on the server.

All the code you posted looks good, I’m not sure what isn’t working.

FYI, you can use Died on the server (and the client too). It might be a bit easier if you do it on the server though:

local ZLife -- SET TO VALUE
local SLife -- SET TO VALUE

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid", 5)
		if not humanoid then --handling case where no humanoid is found.
			return
		end
		humanoid.Died:Connect(function()
			-- if you want the scores to be reduced only during games,
			-- add an if statement here that checks if a game is happening
			if player.Team == game.Teams.Survivor then						

				SLife.Value -= 1		

			elseif player.Team == game.Teams.Zombie then
		
				ZLife.Value -= 1		
		
			end
		end)
	end)
end)

EDIT:
Whoops! I see what’s wrong with your code. When a Humanoid dies, it’s removed (along with the character). You need to get the humanoid of the NEXT/new character then reconnect the Died event. You can get the characters with Player.CharacterAdded.

For example, in the code above, every time a character is added I create a new connection to Humanoid.Died (for a the new Humanoid though).

1 Like

i noticed that too after i posted this I saw another post with the same solution thank you for explaining and giving a solution!

1 Like

is this all done in the Local script or in the Server script?

local RS = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")
local Events = RS:WaitForChild("Events")
local LC = Events:WaitForChild("LivesChange")
local Stat = RS:WaitForChild("Stats")
local SLife = Stat:WaitForChild("SLivesLeft")
local ZLife = Stat:WaitForChild("ZLivesLeft")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()	
	if player.Team == Teams.Survivor then						
		LC:FireServer(SLife)				
	elseif player.Team == Teams.Zombie then
		LC:FireServer(ZLife)		
	end		
end)
local RS = game:GetService("ReplicatedStorage")
local Events = RS:WaitForChild("Events")
local LC = Events:WaitForChild("LivesChange")

LC.OnServerEvent:Connect(function(plr, teamValue)
	teamValue.Value -= 1
end)

You don’t even need one of the remotes for this.

yea I overthought it I didn’t know you could use .died in a server script

Yeah, I’m not sure why you got this misleading reply, .Died will fire in both server scripts and local scripts.