I have a server script that uses this:
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character)
It activates whenever a player respawns after dieing. That's fine. However in my place there's a section where the player can reload his character using
:LoadCharacter()
And the script above still activates. I only want the script to activate after the player respawns by DIEING, not by reloading his character.
What you can do is keep track of whether or not the character died between characters added. To do this, I suggest using the event Died of the object Humanoid.
game.Players.PlayerAdded:connect(function(player) local died = false local firstEntry = true player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() died = true -- If player died, change variable end) if firstEntry then -- If you want to do something on entry print("Welcome to " .. game.Name .. ", " .. player.Name .. "!") firstEntry = false else if died and not firstEntry then -- If the player died then print(player.Name .. " has been resurrected!") died = false else -- If the player didn't die then print(player.Name .. " has been refreshed!") end end end) end)
game.Players.PlayerAdded:connect(function(player) game.Player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() --Code end) end) end)
The Died event happens whenever the character dies so it makes it so instead of every time they respawn it's only when they die. If you wish to make it so you also wait until they respawn and not right as they die then before the code (but still inside the Died event) you should put
repeat wait() until player.Character