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.
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.)
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)
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.
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.
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.
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.
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