Clientside Script Issue

I’m having an issue with player methods of the local player being nil and I’m not sure why it’s happening.

One line of code is this, and it’s ran after PlayerInitialSpawn:


Avatar:SetSteamID( LocalPlayer():SteamID64(), 64 )

It’s saying SteamID64 is a nil value. I tried to change it to SteamID to see if there was a problem with that particular method, and I have the same problem. Same goes for methods I created previously to this script being ran with GetMetaTable(“Player”). I notice that if I run this script again manually, it works.

My guess if that the player entity isn’t fully initialized somehow? I really don’t know tbh. Any help would be appreciated.

PlayerInitialSpawn is a server side hook/function, therefore you must be running this server side.
You can’t get LocalPlayer() via server side.
What is this AvatarImage being used for?

Ah, that does look like a nicer way to set the Avatar, but my issue persists:


surface.DrawText("Cash: £"..tostring(LocalPlayer():Get("Monies", 0.00)))

“Get” is returning a nil value too. If I try and call SteamID, it throws the nil value error also when it shouldn’t.

Get is defined in another clientside script, and the above one is hooked in after it initializes; Get is defined as so:


local plyMeta = FindMetaTable("Player")

function plyMeta:Get(key, default)
	return self.Data and self.Data[key] or default or nil
end

But the issue is not just with ‘Get’. SteamID throws a nil value error too.

[editline]27th June 2014[/editline]

PlayerInitialSpawn is run serverside, and sends a Net message to the client to tell it to initialize the clientside script.

Add a check for IsValid( LocalPlayer() ).

You don’t need the “or nil” part there:



local plyMeta = FindMetaTable("Player")

function plyMeta:Get(key, default)
	return self.Data and self.Data[key] or default or nil
end

Make sure self.Data actually exists on client. Setting self.Data on server won’t make it magically appear on client.

Sorry for the misunderstanding, I didn’t realize you were using LocalPlayer() server side. The way I figured out that you’re using this server side is because PlayerInitialSpawn is a server side function and you said “One line of code is this, and it’s ran after PlayerInitialSpawn:”.
Unless it is a misinterpretation…

Yeah sorry, I may not have explained in the best way. Here is the main part of cl_profilex_master.lua



//### Drawing ###

local function DermaTest(profilePic)
	local Avatar = vgui.Create( "AvatarImage", frame )
	Avatar:SetSize( 64,64 )
	Avatar:SetPos( 10,0 )
	Avatar:SetPlayer( LocalPlayer(), 64 )
end

local function RenderProfileTab()
	if not IsValid(LocalPlayer()) then return end
	local scrW, scrH = ScrW(), ScrH()
	surface.SetDrawColor(Color(50,50,50,225))
	surface.DrawRect(0, 0, 250, 85)
	surface.SetFont("ProfileXStat");
	surface.SetTextPos(100, 10)
	surface.SetTextColor(color_white)
	surface.DrawText(string.sub(ply:Nick(),1,15))
	surface.SetFont("ProfileXStat1");
	surface.SetTextPos(100, 35)
	surface.DrawText("Cash: £"..tostring(ply:Get("Monies", 0.00)))
	surface.SetFont("ProfileXStat1");
	surface.SetTextPos(100, 55)
	surface.DrawText("Rank: "..ply:EV_GetRank())
end

local function HUDPaint()
	RenderProfileTab()
end
//### Main ###

local function Init()
	DermaTest()
end

hook.Add("Dynasave_PostInit", "DynasavePostInitProfileX", function() // This is a hook from a plugin called Dynasave - it is called after the plugin has initialized, and defined 'Get' etc.
Init()
end)


//### Hooks ###
hook.Add("HUDPaint", "ProfileXHUDPaint", HUDPaint)


Even with the Validity check on the LocalPlayer, I get:


[ERROR] addons/profilex/lua/cl_profilex_master.lua:71: attempt to call method 'Get' (a nil value)
  1. RenderProfileTab - addons/profilex/lua/cl_profilex_master.lua:71
   2. v - addons/profilex/lua/cl_profilex_master.lua:78
    3. unknown - lua/includes/modules/hook.lua:84