LocalPlayer only works on LocalScripts, and LocalScripts only work when they are client-sided.
this error is happening exactly because of this: you are trying to use localplayer/localscript server-side, not client-side. for more information, read LocalScript (Devhub) and this Players.LocalPlayer(Devhub)
As I said, LocalScript or LocalPlayer doesn’t work on server-side, or non-localscript, as there’s no way the Roblox engine can detect the local player. and as it does not work, it returns nil
Oh, I thought it was a local script. Yeah, the local player doesn’t work on the server. But you can do it simply by connecting the event:
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
print(character) -- Prints the character to make sure that it exists.
end)
This script should be the solution to your issue. Sorry for the misconception as I didn’t know what script you were using. I presumed it was a local script by the .LocalPlayer instance. Very sorry about that.