I need to wait for Players.LocalPlayer to replicate?

Hi. While checking scripts I found this code inside a modulescript in a tool.

local localPlayer = Players.LocalPlayer
while not localPlayer do
	Players.PlayerAdded:Wait()
	localPlayer = Players.LocalPlayer
end

It suggests that it’s possible that Players.LocalPlayer could be nil. This is possible?

1 Like

No. Especially in a ModuleScript where the code does not run until it’s required, this code is redundant.

2 Likes

This was either made before roblox changed the loading order or is just poorly scripted, but when directly accessing the LocalPlayer, of-course from a local script, you do not need to do that: (When to wait for replication to the client - this announcement clarifies that the LocalPlayer will never be nil, " 'game.Players.LocalPlayer’ will always be non-nil")

A fairly complete answer. Thank you both for helping me.

1 Like