So I have this error:
[CODE][ERROR] gamemodes/sandbox/gamemode/cl_init.lua:444: attempt to index global 'Pla
yer' (a function value)
1. DoClick - gamemodes/sandbox/gamemode/cl_init.lua:444
2. unknown - lua/vgui/dlabel.lua:206[/CODE]
And this error:
[CODE][ERROR] gamemodes/sandbox/gamemode/cl_init.lua:444: attempt to call method 'Give
' (a nil value)
1. DoClick - gamemodes/sandbox/gamemode/cl_init.lua:444
2. unknown - lua/vgui/dlabel.lua:206[/CODE]
I tried this: [CODE]ply:Give("weapon_crowbar")[/CODE] But that didn't seem to work!
Here is my code:
[CODE]local button2 = vgui.Create( 'DButton' )
button2:SetParent( gra )
button2:SetSize( 130, 25 )
button2:SetPos( 159, 200 )
button2:SetText( '' )
button2.DoClick = function()
Player:Give("weapon_crowbar")
end[/CODE]
- Thanks!
Player is not defined in your code, so Lua automatically assumes you want the Global function Player().
What you are doing in your DoClick is:
Function:Function( "" )
[QUOTE=Robotboy655;46387434]Player is not defined in your code, so Lua automatically assumes you want the Global function Player().
What you are doing in your DoClick is:
Function:Function( "" )[/QUOTE]
How would I go about defining the player?
Also, ENT:Give is server-side only. You're going to need to network something to the server and let the server give you the weapon: [url]http://wiki.garrysmod.com/page/Player/Give[/url]
[editline]1st November 2014[/editline]
[QUOTE=Mr.Mezzy;46387451]How would I go about defining the player?[/QUOTE]
On CLIENT the player is always accessible via LocalPlayer( ), however there are instances when the player is first joining where LocalPlayer( ) will be NULL / nil, so you'd need to test for that. Example: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html[/url]
player:Give() is server side, not client side :v:
Put this code on top
[CODE]local Player = FindMetaTable( "Player" )[/CODE]
Accessing the meta-table is something entirely different than what is required here unless you're suggesting he write a clientside Give function which networks the data to the server ( not recommended to ever trust user-input because that would give the client access to any entity that can be given to a player ).
Here's an example of networking data to the server and getting a response ( in terms of identifying a player ): [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/networking/networking_player_identity_from_client_to_server_and_back.lua.html[/url]
You won't need all of that, you'd need a simple 1 way message to the server ( I'd recommend keeping a SHARED table of codes that can be sent to the server [ so we're not trusting user input ] so the server can verify that the code exists and what it is supposed to do )...
Ok couldn't I just set up a command for this? Something like:
[CODE]concommand.Add( "spawnpaint",function( ply )
Player:Give( 'weapon_spraymhs' )
end )
[/CODE]
[lua]
// Server side
util.AddNetworkString("spawn_paint")
net.Receive("spawn_paint", function(len, client)
if !client:HasWeapon("weapon_spraymhs") then
client:Give("weapon_spraymhs")
end
end)
// Client side
function SpawnPaint()
net.Start("spawn_paint")
net.SendToServer()
end[/lua]
Now use SpawnPaint to give player the paint thing clientside
[QUOTE=Author.;46387533][lua]
// Server side
util.AddNetworkString("spawn_paint")
net.Receive("spawn_paint", function(len, client)
if !client:HasWeapon("weapon_spraymhs") then
client:Give("weapon_spraymhs")
end
end)
// Client side
function SpawnPaint()
net.Start("spawn_paint")
net.SendToServer()
end[/lua]
Now use SpawnPaint to give player the paint thing clientside[/QUOTE]
Well, when I clicked the button it didn't give me my paint.
What's the code?
Actually, I do have something in line with what you need: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_zipped_code/acecool_npc_jointeam_example.rar[/url]
[code]// Client side of sh_npc_jointeam.lua
_bjoin.DoClick = function( )
net.Start( "npc_jointeam_cl_to_server_join_team" );
net.SendToServer( );
end
// Server side of sh_npc_jointeam.lua
util.AddNetworkString( "npc_jointeam_cl_to_server_join_team" );
//
// This is the serverside receiver for what the client sends to the server...
//
net.Receive( "npc_jointeam_cl_to_server_join_team", function( _len, _p )
// Since we know the client had to press the join button... Just join..
_p:KillSilent( );
_p:SetTeam( 2 );
_p:Spawn( );
_p:PrintMessage( HUD_PRINTTALK, "You've been set to Team 2; hopefully someone remembered to create it!" );
end );
[/code]
That covers the DoClick networking to server, the server setup for net message, the net receive on the server and actions performed on the server. To expand it, set up a shared table such as:
[code]NETWORKING_WEAPON = 0;
NETWORKING_TEAM = 1;
WEAPON_CROWBAR = 0;
WEAPON_PISTOL = 1;
JOINTEAM_SPECTATOR = 0;
JOINTEAM_ALIVE = 1;
NETWORKING_ACTIONS = {
[ NETWORKING_WEAPON ] = {
[ WEAPON_CROWBAR ] = function( _p )
_p:Give( "weapon_crowbar" );
end;
[ WEAPON_PISTOL ] = function( _p )
_p:Give( "weapon_pistol" );
end;
};
[ NETWORKING_TEAM ] = {
[ JOINTEAM_SPECTATOR ] = function( _p )
_p:SetTeam( TEAM_SPECTATOR );
_p:KillSilent( );
end;
[ JOINTEAM_ALIVE ] = function( _p )
_p:SetTeam( 1 );
_p:Spawn( );
end;
};
};
[/code]
Network 2 ints, here's how to count bits: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/__documentation/bit_counting.lua[/url]
So WriteInt( NETWORKING_WEAPON, 3 ); WriteInt( WEAPON_CROWBAR, 3 ); -- extra bit because a signed integer ( means that the - or + is sent with it ) requires a bit.
The server then looks up the table to see if NETWORKING_WEAPON exists, then if WEAPON_CROWBAR exists, then passes the player in... This is just a rushed simple version of how you can set something up ( it is still somewhat trusting user input because you can select any weapon. You'd add checks in the function to ensure they can get it, etc... )
[QUOTE=Mr.Mezzy;46387517]Ok couldn't I just set up a command for this? Something like:
[CODE]concommand.Add( "spawnpaint",function( ply )
Player:Give( 'weapon_spraymhs' )
end )[/CODE][/QUOTE]
You could ( if it is serverside ) but using console commands as a means to network is typically frowned upon..
[QUOTE=Acecool;46387587]You could ( if it is serverside ) but using console commands as a means to network is typically frowned upon..[/QUOTE]
That command didn't work. And everything you are showing me is so damn confusing. I didn't think pressing a button to give a weapon would be this confusing!
[lua]
button2.DoClick = function()
function SpawnPaint()
net.Start("spawn_paint")
net.SendToServer()
end
[/lua]
wtf is this. can you even code? remove everything but "SpawnPaint()"
tl;dr make the function outside of the code, then run it inside the code. whenever they press the button the function gets created, not ran.
[QUOTE=Author.;46387607][lua]
button2.DoClick = function()
function SpawnPaint()
net.Start("spawn_paint")
net.SendToServer()
end
[/lua]
wtf is this. can you even code? remove everything but "SpawnPaint()"
tl;dr make the function outside of the code, then run it inside the code. whenever they press the button the function gets created, not ran.[/QUOTE]
Well now, I'm getting an error after doing what you said here is the error and code:
[CODE][ERROR] gamemodes/sandbox/gamemode/cl_init.lua:468: unexpected symbol near ')'
1. unknown - gamemodes/sandbox/gamemode/cl_init.lua:0
[/CODE]
[CODE]local button2 = vgui.Create( 'DButton' )
button2:SetParent( gra )
button2:SetSize( 130, 25 )
button2:SetPos( 159, 200 )
button2:SetText( '' )
button2.DoClick = function()
SpawnPaint()
end[/CODE]
And I only got this error after adding whatever you gave me.
What exactly is line 468? Doesnt look like it relates to my code..
[QUOTE=Author.;46387701]What exactly is line 468? Doesnt look like it relates to my code..[/QUOTE]
[CODE]draw.RoundedBox( 0, 0, 0, w, h, Color( 212, 0, ) )[/CODE]
Fixed it.
Now I have this error:
[CODE]
[ERROR] gamemodes/sandbox/gamemode/cl_init.lua:357: Calling net.Start with unpooled message name [http://goo.gl/qcx0y]
1. Start - [C]:-1
2. SpawnPaint - gamemodes/sandbox/gamemode/cl_init.lua:357
3. DoClick - gamemodes/sandbox/gamemode/cl_init.lua:450
4. unknown - lua/vgui/dlabel.lua:206
[/CODE]
Here's line 357:
[CODE]net.Start( "spawn_paint" )[/CODE]
Did you remember to have this on SERVER SIDE?
[lua]
// Server side
util.AddNetworkString("spawn_paint")
net.Receive("spawn_paint", function(len, client)
if !client:HasWeapon("weapon_spraymhs") then
client:Give("weapon_spraymhs")
end
end)
[/lua]
[QUOTE=Author.;46387784]Did you remember to have this on SERVER SIDE?
[lua]
// Server side
util.AddNetworkString("spawn_paint")
net.Receive("spawn_paint", function(len, client)
if !client:HasWeapon("weapon_spraymhs") then
client:Give("weapon_spraymhs")
end
end)
[/lua][/QUOTE]
Yes
Authors' code is correct. Just make sure everything is included or AddCSLua'd correctly.
[B]in your serverside file:[/B]
[code]
util.AddNetworkString("spawn_paint")
net.Receive("spawn_paint", function(len, client)
if !client:HasWeapon("weapon_spraymhs") then
client:Give("weapon_spraymhs")
end
end)
[/code]
[B]In your client side file:[/B]
[CODE]
local button2 = vgui.Create( 'DButton' )
button2:SetParent( gra )
button2:SetSize( 130, 25 )
button2:SetPos( 159, 200 )
button2:SetText( '' )
button2.DoClick = function()
net.Start("spawn_paint")
net.SendToServer()
end[/CODE]
[lua]Calling net.Start with unpooled message name ([url]http://goo.gl/qcx0y[/url])[/lua]
[lua]util.AddNetworkString("spawn_paint")[/lua]
[lua]net.Start( "spawn_paint" )[/lua]
You did something wrong here, son. Because it was not my code for sure.
[QUOTE=Author.;46387864][lua]Calling net.Start with unpooled message name ([url]http://goo.gl/qcx0y[/url])[/lua]
[lua]util.AddNetworkString("spawn_paint")[/lua]
[lua]net.Start( "spawn_paint" )[/lua]
You did something wrong here, son. Because it was not my code for sure.[/QUOTE]
Couldn't I just make a command for giving the weapon?
Actually. Go into your clientside file. Copy and paste the vgui element your using for giving the weapon (including the net.Start) and do the same for the serverside file but instead the code your using for networking and paste it both here: [url]http://pastebin.com/[/url] and paste the link back here
[lua]
// server side code only ok?
concommand.Add("spawn_paint", function(ply, cmd, args)
ply:Give("weapon_spraymhs")
end)
[/lua]
now do RunConsoleCommand("spawn_paint") clientside.
[QUOTE=Author.;46387964][lua]
// server side code only ok?
concommand.Add("spawn_paint", function(ply, cmd, args)
ply:Give("weapon_spraymhs")
end)
[/lua]
now do RunConsoleCommand("spawn_paint") clientside.[/QUOTE]
Thank you. And oh, just to clear something up:
[QUOTE]can you even code?[/QUOTE]
If I could code I don't think I would be on Developer Discussion asking for help. So sorry I don't know how to code as good as you and don't live up to your standards.
But, again. Thanks.
Sorry, you need to Log In to post a reply to this thread.