Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I have a script activate ONLY during respawn?

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.

2 answers

✓ Accepted answer

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)

2 comments

0

It was working a while ago, but now for some reason it's detecting a "died" as a "reload". Might just be a roblox glitch but any ideas on how to fix this?

0

Sorry, that was my bad. I was making it do the Died check on the first character that entered the game instead of the current one. It works now though!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.