function respawnPlayer(player)
if player then
print("Player found")
local character = player:WaitForChild("Character")
if character then
print("Character")
local character = player.CharacterAppearanceLoaded:wait()
gameEvent:FireClient(player, activeValue.Value)
end
end
end
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
gameEvent:FireClient(player, activeValue.Value)
character:WaitForChild("Humanoid").Died:Wait()
print("Died")
if player:WaitForChild("GameFolder").Killer.Value ~= nil then
handleKills(player.GameFolder.Killer.Value, player)
wait(2)
end
if activeValue.Value then
print("Respawn fired")
respawnPlayer(player)
end
end)
end)
What I’m trying to get to happen is when a player dies I want it to fire an event, so if the game is active, then it can make a gui visible. However, when a player joins in the game and resets, nothing happens. When you reset a second time it prints Died, but doesn’t fire the gameEvent. It also prints Respawn fired and Player found on the second reset.
So in short, the problems are:
Died event doesn’t fire on the first reset
It doesn’t seem to waiting for the character to load in the respawnPlayer function
player:WaitForChild(“Character”) does not work as you expect. Character is not a child of Player, so that would timeout. But oddly enough you can use Player.Character to reference the Character model from the workspace from the Player model in the Players service.
Mmmh problem with just doing player.Character is that when I reset the second time, the Gui pops up, but then the player respawns and the Gui is gone again. I don’t know how to make it so I wait for the players character to respawn. Also, resetting the first time still doesn’t fire the Died function
When I have a problem with scripts like this only working after the 2nd death I usually just put player:LoadCharacter() after the CharacterAdded event and before the end of the PlayerAdded event.
The only reason I would see it firing only after the second death is because its not reaching the .CharacterAdded function fast enough, make sure you don’t have any code between the 2 functions just to be safe.
Example of code not reaching CharacterAdded fast enough:
The code above would only work after the second respawn of the character because its not grabbing onto the function fast enough due to the wait.
Otherwise It looks like what you’re doing should work fine, I had an issue with studio ignoring my code once and it was due to some yielding problem. Try debugging it by deleting parts of the code and testing to see which part of it works alone.
On a side note, :connect is deprecated, use :Connect instead.
So, long back I have had issues like this. I used a certain setup to get around it. Seeing as your issue involves code only running the second time, try switching to a layout more like this and see it it helps:
local PlayersService = game:GetService("Players")
local function OnPlayerAdded(Player)
local function OnCharacterAdded(Character)
--Make sure the character exists
if Character then
--code
end
end
Player.CharacterAdded:Connect(OnCharacterAdded)
OnCharacterAdded(Player.Character)
End
PlayersService.PlayerAdded:Connect(OnPlayerAdded)
local Players = PlayersService:GetPlayers()
for i = 1, #Players do
OnPlayerAdded(Players[i])
end
Ever since I switched to something more like that I have never run into issues like this in studio or in-game.
Sorry if I made typos. Typed from phone.
On another note, I have had issues with .Died firing when a player didn’t actually die. I still need to take time out to try and reproduce, but be aware of that.
When trying to get around it in my game, I added a check to see if their health is <= 0, however that check returned true too even without the player dying. My fix around it was to call :BreakJoints on their character if the .Died event fired, since it only misfired when they basically died anyway.