How to make something happen upon spawn?

(like for example, making a certain dialogue play everytime you die and then respawn/spawning in the game, or making it so a part respawns everytime you spawn.)
Can someone just give me a general idea of how to script this? Thanks.

6 Likes

You can use the PlayerAdded, then CharacterAdded functions. The CharacterAdded function runs everytime the Character of a Player spawns.

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		--Code
	end)
end)

The “Player” variable is the player that joined and the “Character” variable is the character of the player

If you want, you can also use a local script.

local Player = game.Players.LocalPlayer
Player.CharacterAdded:Connect(function(Character)
	--Code
end)

This has the exact same logic, The “Player” variable is the player and the “Character” variable is the character of the player
You can put your code in the –Code line. For example;

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid:TakeDamage(50)
	end)
end)

The “Humanoid” of the “Player” will take 50 damage.

I hope my help worked!

4 Likes

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