Dumb PlayerAdded question in LocalScript

I’m trying to listen PlayerAdded() and CharacterAdded() inside a LocalScript.

Works weird, maybe due to the order and times that players, scripts are created…

So, PlayerAdded etc, should be used in ServerSide and use RemoteEvents to call Client’s functions?

4 Likes

Hello, you can use both PlayerAdded() and CharacterAdded() in Server and Client-Sided script.
However, if you are listening to PlayerAdded() in LocalScript, it will only fire when another player joins, not when the player who runs the LocalScript joins.

14 Likes

Yup thats exactly what I noticed. Only listen when another player joined, instead of listening when that client is joining…

So yes? I can only use playerAdded from server when I want to use functions inside the Client’s script that is joining?

May I ask what are you trying to achieve with listening when client joins in LocalScript?
Anyways, if you want to run anything when client join in LocalScript, you can just add directly to LocalScript without listening and waiting for any event. (Depends on the location of LocalScript, but mostly it does.)

1 Like

Thank you for ur reply. Im very confused about the mechanics

Well, Im not trying anything in particular, just testing.
Lets say, I want to execute a function everytime a player join and respawn. And run it from a LocalScript without listening from Server and use a RemoteEvent to call that specific Client

Let’s say you want to print “Dev_Peashine” every time when the client joins or respawns on LocalScript which is placed inside StarterPlayerScripts of StarterPlayer.

local LocalPlayer = game.GetService("Players").LocalPlayer -- Defines variable LocalPlayer as the client
local message = "Dev_Peashine" -- Defines variable message with the message you want to print

print(message) -- Prints message once when the player joins

LocalPlayer.CharacterAdded:Connect(function(character) -- Listens for client's character being newly added to client; mostly when client has respawned or refreshed.
    print(message) -- Prints message once when the character is added to the client.
end)
2 Likes

Yup well xD
I know that.
My question its about the first thing u said:
" if you are listening to PlayerAdded() in LocalScript , it will only fire when another player joins, not when the player who runs the LocalScript joins."

That’s my problem, I wanted to control it’s own join and respawn only from LocalScript in that client.
You told me, that it doesnt run for the player that is joining itself, only for others that already are in the game

That’s not really done on purpose. The reason is local scripts start executing after the player joins. That’s why it doesn’t fire for the player not because it’s done intentionally.

2 Likes

That’s the exact point why LocalScript can’t listen when client joins with PlayerAdded. @Dev_Peashie

2 Likes

I guess I can understand it better. It’s better to listen the join player from server, thats all, right?

Makes totally sense if local scripts execute after the player joins

Are you just looking to fire this event for players who come after the client joins or everyone?

1 Like

Yes, because LocalScript executes after the player joins, you can have code in LocalScript without listening for any event if you want to execute when the player joins as I said in the example above.

1 Like

Each one that joins, listen from LocalScript inside the player that is joining, triggering something from the joining event in that joining client

Well in that case you won’t get the event fired for the clients who joined before the player joined. it depends on your use case if you need this or not.

1 Like

Yeah I guess im just testing nonsense xD
But yeah that how I learn ¯_(ツ)_/¯

Thank you both for ur time btw :3

I mean it can be useful to listen to events from the client, it’s just based on what your doing.

Unless this is just for plain testing purposes.

Yup, but I dont have any goal for this quick test :v

Alright let me explain some things which happen then:

Player 2 Joins. Connects the playeradded listener [Doesn’t fire for himself since this script runs after he joins]

Player 1 joins: [Player 2’s PlayerAdded event fires] Player1 connects his listener [Doesn’t fire for himself since this script runs after he joins] However his PlayerAdded event won’t fire when Player2 joins obviously since Player2 was already there, there was no added player.

waaa I need to read it very carefully, its confusing :v
Thank u for helping to make it clear for me

Ohhh I think I got it. The word connection helped a lot, cause, the moment they got that connection. Well im really bad at explaining… but I think I understand it better after reading everything from u guys. xD Thank u, it helps a lot. Very confusing mechanics :v

But yeah, characterAdded and all other stuff can still be handled very well from LocalScript too, so, I was just wondering about joinedplayer, that was confusing when I tried to call it from Client