LocalPlayer() returning nil ?

Well, i’m using fretta and was checking out an example for a buy menu, but localplayer returns nil for some reason.

[lua]
function GM:ShowBuyMenu()

local client = LocalPlayer()

if ( !IsValid( buyMenu ) ) then
	buyMenu = vgui.CreateFromTable( vgui_Splash )
	buyMenu:SetHeaderText( "The Shop" )
	buyMenu:SetHoverText( "yeah" )
	
	local function1 = function() client:Give( "weapon_shotgun" ) end
	local Button = buyMenu:AddSelectButton( "Shotgun", function1 )
	Button.m_colBackground = Color( 255, 90, 90, 255 )
	
	local function2 = function() client:GiveAmmo( 30, "buckshot" ) end
	local Button2 = buyMenu:AddSelectButton( "Shotgun Ammo", function2 )
	Button2.m_colBackground = Color( 255, 90, 90, 255 )
	
	Button2.Think = function( self )
		self:SetDisabled( !client:HasWeapon( "weapon_shotgun" ) )
	end
	
	buyMenu:AddCancelButton()
end

buyMenu:MakePopup()

end
concommand.Add( “BuyMenu”, function() GAMEMODE:ShowBuyMenu() end )
[/lua]

Uh, that’s weird. But really, you don’t even need LocalPlayer() in there, you can do this instead:

[lua]
function GM:ShowBuyMenu(client)
if ( !IsValid( buyMenu ) ) then
buyMenu = vgui.CreateFromTable( vgui_Splash )
buyMenu:SetHeaderText( “The Shop” )
buyMenu:SetHoverText( “yeah” )

	local function1 = function() client:Give( "weapon_shotgun" ) end
	local Button = buyMenu:AddSelectButton( "Shotgun", function1 )
	Button.m_colBackground = Color( 255, 90, 90, 255 )
	
	local function2 = function() client:GiveAmmo( 30, "buckshot" ) end
	local Button2 = buyMenu:AddSelectButton( "Shotgun Ammo", function2 )
	Button2.m_colBackground = Color( 255, 90, 90, 255 )
	
	Button2.Think = function( self )
		self:SetDisabled( !client:HasWeapon( "weapon_shotgun" ) )
	end
	
	buyMenu:AddCancelButton()
end

buyMenu:MakePopup()

end
concommand.Add( “BuyMenu”, function(pl) GAMEMODE:ShowBuyMenu(pl) end )
[/lua]

Oh, also I just noticed something, I doubt Give and GiveAmmo work clientside, you’ll need to set up a serverside console command, and execute it from the client. Of course, make sure to check how much money the player has (serverside, that is).

I’m not sure why LocalPlayer() is nil (possibly being called serverside) but you could use the player that called the concommand instead:

[lua]concommand.Add( “BuyMenu”, function(ply) GAMEMODE:ShowBuyMenu(ply) end ) [/lua]
[lua]function GM:ShowBuyMenu(ply)[/lua]

then use ply instead of LocalPlayer().

[editline]08:04PM[/editline]

:ninja:

ply doesn’t work either. :saddowns:

ply doesn’t work when using exactly what _Kilburn posted?

yes

Strange.

What error are you getting now?

Obviously, it is still nil.

It is.

I don’t get it, I have other functions that access LocalPlayer just fine on the clientside, yet this one doesn’t work.

Only if you’re calling it before the player is valid.

Are you running that piece of code server or clientside now?

You’ll have to create the buymenu clientside, and do the functionality serverside.

Have you tried adding debug lines like print(LocalPlayer()) to get a better eye on what’s going on? Either way it shouldn’t work - clientside scripts for obvious reasons can’t give a player ammo or weapons. And you should really paste any errors you’re getting.

The only error i’m getting are errors related to nil. LocalPlayer() is nil, therefore Give and HasWeapon are nil.

And yes I have, it’s nil

Oh god I’m so sorry, did I try to help? I swear I’ll never do it again!

LocalPlayer() will never return nil.

It always returns an entity, that entity might be NULL but never nil.

You misinterpreted the error, you are attempting to call a nil value on a table.

Give and GiveAmmo are serverside you are running them clientside which means they don’t exists aka are nil.

:irony:

Seriously don’t insult people for helping you, you might just look really dumb.

Paste the errors to us, don’t just describe them.

Oh god, I’m saving this.

Stop whining and read this. Also, copy-paste the exact errors that you’re getting. Nobody is going to help you if you don’t give every possible detail.

[editline]12:18PM[/editline]

Actually, fuck this, this should work:

Serverside:

[lua]concommand.Add( “buymenu_giveweapon”, function( pl, cmd, args )
pl:Give( args[1] )
end )

concommand.Add( “buymenu_giveammo”, function( pl, cmd, args )
pl:GiveAmmo( tonumber(args[1]), args[2] )
end )[/lua]

Clientside:

[lua]local function PlayerHasWeapon( pl, weapon )
for _,v in pairs( pl:GetWeapons() ) do
if weapon == v:GetClass() then
return true
end
end
return false
end

function GM:ShowBuyMenu( client )
if not IsValid( buyMenu ) then
buyMenu = vgui.CreateFromTable( vgui_Splash )
buyMenu:SetHeaderText( “The Shop” )
buyMenu:SetHoverText( “yeah” )

	local function1 = function() RunConsoleCommand( "buymenu_giveweapon", "weapon_shotgun" ) end
	local Button = buyMenu:AddSelectButton( "Shotgun", function1 )
	Button.m_colBackground = Color( 255, 90, 90, 255 )
	
	local function2 = function() RunConsoleCommand( "buymenu_giveammo", 30, "buckshot" ) end
	local Button2 = buyMenu:AddSelectButton( "Shotgun Ammo", function2 )
	Button2.m_colBackground = Color( 255, 90, 90, 255 )
	
	Button2.Think = function( self )
		self:SetDisabled( not PlayerHasWeapon( client, "weapon_shotgun" ) )
	end
	
	buyMenu:AddCancelButton()
end

buyMenu:MakePopup()

end

concommand.Add( “BuyMenu”, function( pl )
GAMEMODE:ShowBuyMenu( pl )
end )
[/lua]

It’s untested, but I don’t think I’ve missed anything. Player.HasWeapon doesn’t exist clientside either, so you have to simulate it using Player.GetWeapons.

Just tested your method, works fine for me.