LocalPlayer() returning [Null Entity]

Allright guys, so anyone care to explain to me whats going on? This code was working a few hours ago, and nothing was changed as it was one of the very first pieces of code for this game mode that I coded.

[lua]

net.Receive( “SetupPlayer”, SetupPlayer)
function SetupPlayer()
local ply = LocalPlayer()
print(ply) – Returns Null

ply.data = {}
ply.data.Resources = {} -- Error: Attempt to Index table "data" a nill value
ply.data.stats = {}
ply.data.skills = {}
ply.bodyArmor = {}
ply.bodyArmor["Tier"] = 0

end
[/lua]

What the hell is going on?

Not the best at Lua. But make sure that LocalPlayer is being run client side.
if CLIENT then LocalPlayer() stuff end
If you just want to get the players from the server you can always do like a player.GetAll() and loop through them.

Of course it is being clientside. Its being called off of a server net.Send

Isn’t net.recieve like a function in itself? So that’s like making a function inside of a function. Remove the function and try.

No. It works perfectly fine that way. It’ll call the function instead. But, I managed to find the issue. I had just added a timer to the call. This Serverside version was being called on PlayerInitialSpawn to send this net hook. Added a timer of 0.1 seconds and its all good. Really fucking annoying.

I still don’t understand why you’d put a function inside of a net receive…

Because I can?

It seems fairly useless and unoptimized, when you can just call net.Send again…

Why send data twice, when you can just wait to send it the first time? Did you even think that through?

[editline]18th January 2013[/editline]

What happened to all the Lua coders who actually KNEW what they were talking about? :confused:

[editline]18th January 2013[/editline]

I also realized I never answered your question, because I honestly thought you were just trolling me or some shit. You pass in the function as a parameter to call said function. If you create the function inside the parameter itself, the function is still getting localized so that it may be called again on later. The difference between my method and yours, is that I may still call this function outside of this receive hook, rather, with yours, I cant. On top of that, yours is doing exactly what I am doing in the background anyway.

It seems like there’s a period of time (depending on the computer speed, network stress, entities or something idk) after PlayerInitialSpawn where the client still isn’t fully initialized. I couldn’t find any documentation for it so I’m not sure of what exactly it does but try toying with pl:IsFullyAuthenticated() serverside as well.

And that is exactly why scripting for GM13 is such a clusterfuck. No documentation.

I’m pretty sure LocalPlayer() returns NULL during full updates and before you’re spawned.

Yup. Timer fixed that. Thanks :smiley: