Script that happens when player dies doesn't work

The script below is supposed to occur when the player dies, putting them into a team. It doesn’t work at all. Why does this not work? What is the issue? Thanks in advance!

Keep in mind this is a script, not a local script.

local player = game:GetService("Players").PlayerAdded:Wait()
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local teams = game:GetService("Teams")
local playerobject = player.TeamColor

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		while true do
			if humanoid.Health == 0 then
				player.Team = teams["Players"]
				character.Parent = workspace.PlayerWaitingFolder
			end
			wait(1)
		end
	end)
end)
2 Likes

try humanoid.Died:Connect() instead (but not in a loop)

3 Likes

tried it, also edited my script a little. didn’t work though

local player = game:GetService("Players").PlayerAdded:Wait()
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local teams = game:GetService("Teams")
local playerobject = player.TeamColor

game:GetService("Players").PlayerAdded:Connect(function(player)
	humanoid.Died:Connect(function(character)
		while true do
			if humanoid.Health == 0 then
				player.Team = teams["Players"]
				character.Parent = workspace.PlayerWaitingFolder
			end
			wait(1)
		end
	end)
end)
local player = game:GetService("Players").PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local teams = game:GetService("Teams")
local playerobject = player.TeamColor

humanoid.Died:Connect(function(character)
	player.Team = teams["Players"]
	character.Parent = workspace.PlayerWaitingFolder
end)

not sure if this is exactly what you’re trying to do, but it should work

2 Likes

This worked! Thank you! I didn’t know it was as simple as that. :sob:

1 Like
local player = game:GetService("Players").PlayerAdded:Wait()

PlayerAdded:Wait() doesn’t give you the player that joined, it simply just waits for a player to join the game, then continues the script.

Also, parenting the character to a folder won’t do anything because the character is going to be deleted
Try this script:

local teams = game:GetService("Teams")
local PlayersWaiting = {}

game:GetService("Players").PlayerAdded:Connect(function(player: Player)
	player.CharacterAdded:Connect(function(character: Model)
		repeat task.wait() until character:FindFirstChild("Humanoid")
		local humanoid: Humanoid = character.Humanoid
		humanoid.Died:Connect(function() 
			player.Team = teams["Players"]
			table.insert(PlayersWaiting, player)
		end)
	end)
end)

The parenting was supposed to be part of a system for counting (I have a timer that goes off, showing the number of children in that folder which are the players.) Thanks for the explanation on the PlayerAdded:Wait().

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.