Im trying to make it so a function happens every time a player respawns or joins through a local script. Please let me know
COMMUNITY QUESTION
How would i make this happen whenever a player joins or respawns?
1 answer
Here is code that may help you:
local PlayerService = game:GetService("Players")
local function onCharacterAdded(character)
--Run respawn code here
end
local function onPlayerAdded(player)
--Run join code here
player.CharacterAdded:Connect(onCharacterAdded)
end
PlayerService.PlayerAdded:Connect(onPlayerAdded)
What it does: it gets the service called Players and then connects an event called PlayerAdded under Players to a function we made called onPlayerAdded. Then, in the onPlayerAdded function, we use the player instance obtained by the player parameter included in the PlayerAdded event to connect an event called CharacterAdded, which is fired when the player's character respawns, to a function we made called onCharacterAdded.
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
where should i run it in. a part or?
should work anywhere as long as it's a server sided Script and is under Workspace or ServerScriptStorage. You could use a LocalScript for it but the PlayerAdded event wont fire for your own Player, since by the time the LocalScript runs your Player is already in the game.
i need it to be used in a local script because i need the function to do something to themself and not others
Localscripts can only run under ReplicatedFirst and player. ReplicatedFirst runs immediately as you load in and player scripts take a little bit longer.