Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would i make this happen whenever a player joins or respawns?

Asked by 3 years ago

Im trying to make it so a function happens every time a player respawns or joins through a local script. Please let me know

1 answer

Log in to vote
1
Answered by 3 years ago

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 onPlayerAddedfunction, we use the playerinstance obtained by the playerparameter included in the PlayerAddedevent to connect an event called CharacterAdded, which is fired when the player's characterrespawns, to a function we made called onCharacterAdded.

0
where should i run it in. a part or? TeaWithMee 76 — 3y
0
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. oilsauce 196 — 3y
0
i need it to be used in a local script because i need the function to do something to themself and not others TeaWithMee 76 — 3y
0
Localscripts can only run under ReplicatedFirst and player. ReplicatedFirst runs immediately as you load in and player scripts take a little bit longer. RetroGalacticGamer 331 — 3y
Ad

Answer this question