Code works, but still errors?

So right now im currently trying to set a cookie with a players Steam ID and Name.

The code itself works, and sets the cookie, however, i still get this error:


25: attempt to call method 'SteamID' (a nil value)


The line it is talking about is this one:


if (!dec[tostring(LocalPlayer():SteamID())])

can someone explain to me why this is happening? Thanks.

LocalPlayer() only returns the LocalPlayer after the InitPostEntity hook is called, so you have to wait until after that hook is called.

Make sure the LocalPlayer is valid; you could be running the code before it is.

[editline]23rd August 2015[/editline]

Ninja’d

Alright, thanks!

EDIT: How would i go about checking this?

Would I do something like this:


if ( IsValid( LocalPlayer() ) and LocalPlayer():IsPlayer() ) then

You don’t need the IsPlayer check.

so i did


if (IsValid(LocalPlayer())) then

before local checkCookieplz, and now the code isn’t working anymore.

Then you need to run it past InitPostEntity, meaning when LocalPlayer() is valid.

Can I get an example of how to do this?(sorry, I’m still new to this LUA scene.)

PlayerInitialSpawn hook.

I currently have this:


hook.Add("PlayerInitialSpawn", "SetTheCookieSelf", GiveTheCookiePly)

I’ve had this the entire time, even while I was having the error.

Create a local var ply outside of any scopes in your code. Then hook.Add InitPostEntity and set the ply variable to LocalPlayer() in there. Where you have to use the ply variable, add IsValid checks so that the code only runs when you have access to LocalPlayer.

You just said exactly what code_gs and Melted Bu11et already said…/