How would I check if player dies?

So, I have a serverscript that only checks when the player joins the game; The only problem is that I want it to check when the player changes their team and resets. I will provide the code below, how would I do this?

Code

local TeamService = game:GetService("Teams")
local GroupID = 10158175
local Player = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(player)
	if player.Team == TeamService["KGB"] and player:IsInGroup(GroupID) then
		ReplicatedStorage.Tools.Test = Player.Character
	else
		print("Player is not in group and/or is not on the KGB team")
	end
end)

You’d use Humanoid.Died event that fires every time the player dies - and by getting their humanoid, you’d need to link a CharacterAdded event.

Can you send me that bit of code?

1 Like

game.Players.PlayerAdded:Connect(function(player)
player.Character:WaitForChild('Humanoid').Died:Connect(function()
-- Do stuff here
end)
end)

1 Like
game.Players.PlayerAdded:Connect(function(plr)
   plr.CharacterAdded:Connect(function(char)
       local hum = char:WaitForChild("Humanoid")
       hum.Died:Connect(function()
           --code here
       end)
   end)
end)

Would this work? Don’t mind the spacing, my keyboard is acting up.\

Ehh… Im not really sure about it, but why don’t you try to put your code inside the CharacterAdded function?

That’d work. If you’re cloning the tool from ReplicatedStorage, don’t forget to add :Clone().Parent.

I re did it, sorry about that. It still doesn’t seem to work though.

Is there anyway you could rewrite my code for me, I keep getting errors and it doesn’t work.

Here is is

game.Players.PlayerAdded:Connect(function(player)
     player.Character:WaitForChild('Humanoid').Died:Connect(function()
          -- Do stuff here for the player
     end)
end)

I got this error.

ServerScriptService.Checker2:8: attempt to index nil with 'WaitForChild’

Also, where I would I put the :Clone() part at?

That means the character hadn’t loaded yet. Try this:

game.Players.PlayerAdded:Connect(function(player) 
     player.CharacterAdded:Connect(function(character) 
           character:WaitForChild('Humanoid').Died:Connect(function() 
                    -- Do stuff here
          end)
     end)
end)
--Script in ServerScriptService
local teams = game:GetService("Teams")
local rep = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(plr)
	print("player added")
	plr.CharacterAdded:Connect(function(char)
		print("character added")
		local hum = char:WaitForChild("Humanoid")
		hum.Died:Connect(function() 
			print("died")
			if plr.Team == teams["KGB"] and plr:IsInGroup(10158175) then
				print("validated, cloning..")
				rep.Tools.Test:Clone().Parent = plr.Backpack
			end
		end)
	end)
end)

I think the reason it isn’t working is because the system I use for resetting is refreshing. Instead of resetting the character, it quickly just refreshes them.

Well, does it work when actually resetting? If so, provide me the refreshing script.

Actually, implement this into your refreshing script where the player is refreshed:

			if plr.Team == teams["KGB"] and plr:IsInGroup(10158175) then
				print("validated, cloning..")
				rep.Tools.Test:Clone().Parent = plr.Backpack
			end
local function loadCharacter(client)
	if client then
		if client.Character ~= nil then
		else
			client:LoadCharacter()
		end
	end
end
CharacterLoader.OnServerEvent:Connect(loadCharacter)

You should make it easier by using a
StarterCharacter Script saying

local TeamService = game:GetService("Teams")
local hum= script.Parent.Humanoid
local GroupID = 10158175
local Player = game.Players:GetPlayerFromCharacter(script.Parent)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

hum.Died:Connect(function()
           if plr.Team == teams["KGB"] and plr:IsInGroup(10158175) then
				print("validated, cloning..")
				rep.Tools.Test:Clone().Parent = plr.Backpack
			end
       end)

Alright, do this:

local function loadCharacter(client)
	if client then
		if client.Character ~= nil then
		else
			client:LoadCharacter()
			if plr.Team == teams["KGB"] and plr:IsInGroup(10158175) then
				print("validated, cloning..")
				rep.Tools.Test:Clone().Parent = plr.Backpack
			end
		end
	end
end
CharacterLoader.OnServerEvent:Connect(loadCharacter)

into your refreshing script as well.