LocalPlayer() Tried to use a NULL entity!

I’m turing the sandbox gamemode into a new roleplay ish gamemode to take control over everything,

But somehow in he spawnmenu, something weird happens when I try to call my rank using GetNWString


LocalPlayer():GetNWString("rank")

This gives an error:


gamemode/spawnmenu/creationmenu/content/contenttypes/weapons.lua:70: Tried to use a NULL entity!
  1. GetNWString - [C]:-1


What am I doing wrong?

This is all clientside

We don’t have a whole lot of code to go by, just one line and an error. Give us a snippet.


hook.Add( "PopulateWeapons", "AddWeaponContent", function( pnlContent, tree, node )

	-- Loop through the weapons and add them to the menu
	local Weapons = list.Get( "Weapon" )
	local Categorised = {}
		
	-- Build into categories
	for k, weapon in pairs( Weapons ) do
			
		if ( !weapon.Spawnable ) then continue end

		Categorised[ weapon.Category ] = Categorised[ weapon.Category ] or {}
		table.insert( Categorised[ weapon.Category ], weapon )
		
	end

	Weapons = nil

	-- Loop through each category
	for CategoryName, v in SortedPairs( Categorised ) do
		
		-- Add a node to the tree
		local node = tree:AddNode( CategoryName, "icon16/gun.png" );
				
		-- When we click on the node - populate it using this function
		node.DoPopulate = function( self )
	
			-- If we've already populated it - forget it.
			if ( self.PropPanel ) then return end
		
			-- Create the container panel
			self.PropPanel = vgui.Create( "ContentContainer", pnlContent )
			self.PropPanel:SetVisible( false )
			self.PropPanel:SetTriggerSpawnlistChange( false )
		
			for k, ent in SortedPairsByMemberValue( v, "PrintName" ) do
									
				spawnmenu.CreateContentIcon( ent.ScriptedEntityType or "weapon", self.PropPanel, 
				{ 
					nicename	= ent.PrintName or ent.ClassName,
					spawnname	= ent.ClassName,
					material	= "entities/"..ent.ClassName..".png",
					admin		= ent.AdminOnly
				})
			
			end
	
		end
		
		-- If we click on the node populate it and switch to it.
		node.DoClick = function( self )
	
			self:DoPopulate()		
			pnlContent:SwitchPanel( self.PropPanel );
	
		end
		
	end


	-- Select the first node
	local FirstNode = tree:Root():GetChildNode( 0 )
	if ( IsValid( FirstNode ) ) then
		FirstNode:InternalDoClick()
	end

end )

if ( LocalPlayer() and LocalPlayer():GetNWString("rank") == "owner" ) then
	spawnmenu.AddCreationTab( "#spawnmenu.category.weapons", function()

		local ctrl = vgui.Create( "SpawnmenuContentPanel" )
		ctrl:CallPopulateHook( "PopulateWeapons" );
		return ctrl

	end, "icon16/gun.png", 10 )
end

It’s in:
gamemodes/sandbox/gamemode/spawnmenu/creationmenu/content/contenttypes/weapons.lua

LocalPlayer doesn’t exist at that time.

Hm, How is it posible? Do I need to move them after the initialization?

I thought that you could access LocalPlayer() only after GM:InitPostEntity() was called

i.e. after all ents have been initialized