I was trying to make a camera interpolation when Player joins the game in a local script inside of StarterGui. I tried using PlayerAdded event in that local script and then print something when player is added, but that seems not working. My output is empty.
Here is the code.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
print("Player is added")
end)
I’ve tried searching on YouTube, scriptinghelpers, Roblox Wiki, but these sites didn’t help. I was reading on ROBLOX Wiki about Player Added event and there’s a note which says “Up until recently, this event didn’t work on the client (in Localscript s), but this has been changed”
I just can’t find the problem. Does anybody knows how can I fix this?
Local scripts run after the player is created. This means that the PlayerAdded event will not fire for the local player joining. Instead, just put the code that would otherwise be under PlayerAdded in the local script and it will run when the player joins.
On top of this, the Player instance itself can be got through Players.LocalPlayer, which will be available immediately in a LocalScript.
OP - You do not need to listen for the player being added on the client. By definition, when LocalScripts run on your machine, you’ve already been added to the game.
So, there’s a chance that Players.LocalPlayer will be nil at some given time in a LocalScript? If so, are there any rules that dictate when the property will and won’t be nil? I’ve always thought that the property will never be nil in a LocalScript.
No. I’m not sure why that code was posted either (the one about waiting until the existence of the player via a repeat statement). That’s a relatively pointless measure. LocalScripts only run after LocalPlayer is initialised. LocalPlayer is implicitly available to LocalScripts.
LocalScripts are still replicated and ran after LocalPlayer is completely set up, same as before.
LocalScripts should be detecting game.Players.LocalPlayer without issues when it starts. If LocalPlayer is nil when a localScript starts, it would present an issue - please report this case.
LocalPlayer is always available to me even in ReplicatedFirst scripts. If it’s not, I believe it’s a bug - LocalPlayer should be implicitly available in any and all LocalScripts.