What's wrong with my newbish code?

2 Files, No idea how to fix: \lua\autorun\client\cl_commands.lua



function ShowGui(msg)

-- Lua generated by DermaDesigner

local SMG
local Pistol
local DFrame1

local DFrame1 = vgui.Create('DFrame')
DFrame1:SetSize(270, 99)
DFrame1:SetPos(221, 108)
DFrame1:SetTitle('GUUUUNZ')
DFrame1:SetSizable(true)
DFrame1:SetDeleteOnClose(false)
DFrame1:MakePopup()

local Pistol = vgui.Create('DButton')
Pistol:SetParent(DFrame1)
Pistol:SetSize(70, 25)
Pistol:SetPos(34, 53)
Pistol:SetText('Pistol')
Pistol.DoClick = function() 
RunConsoleCommand("giveme 1")
end

local SMG = vgui.Create('DButton')
SMG:SetParent(DFrame1)
SMG:SetSize(70, 25)
SMG:SetPos(165, 54)
SMG:SetText('SMG')
SMG.DoClick = function()
RunConsoleCommand("giveme 2")
end

end
usermessage.Hook("psay_command",ShowGui) -- Doesn't work!


Server side: \lua\autorun\server\sv_commands.lua




function WeaponsG(pl,cmd,args)
if (args[1] == 1) then
pl:Give("weapon_pistol")
elseif (args[1] == 2) then
pl:Give("weapon_smg")
end
end
concommand.Add("giveme",WeaponsG)

function Say(pl,text)
if (string.Left(text,1) == "/") then
if (text == "/weaponmenu") then
umsg.Start("psay_command",pl) -- This seems to not be working either
umsg.End()

end
return ""
end

end

hook.Add("PlayerSay","saybitch",Say)


Problem: The Hook doesn’t Work, doesnt show the gui, and I get a warning:




Warning: Unhandled usermessage 'psay_command'


server
[lua]
concommand.Add(“giveme”, function(ply, cmd, args)
if( args[1] == tostring( 1 ) ) then
ply:Give(“weapon_pistol”);
elseif( args[1] == tostring( 2 ) ) then
ply:Give(“weapon_smg”);
end
end)

hook.Add(“PlayerSay”, “ChatCommands”, function(ply, text, public)
local chattab = string.Explode(" ", text);

if( chattab[1] == "/weaponmenu" ) then
	umsg.Start("psay_command", ply); umsg.End();
end

end)
[/lua]

client
[lua]
usermessage.Hook(“psay_command”, function( data )
local DFrame1 = vgui.Create(‘DFrame’)
DFrame1:SetSize(270, 99)
DFrame1:SetPos(221, 108)
DFrame1:SetTitle(‘GUUUUNZ’)
DFrame1:SetSizable(true)
DFrame1:MakePopup()

local Pistol = vgui.Create('DButton')
Pistol:SetParent(DFrame1)
Pistol:SetSize(70, 25)
Pistol:SetPos(34, 53)
Pistol:SetText('Pistol')
Pistol.DoClick = function() 
	RunConsoleCommand("giveme 1")
end

local SMG = vgui.Create('DButton')
SMG:SetParent(DFrame1)
SMG:SetSize(70, 25)
SMG:SetPos(165, 54)
SMG:SetText('SMG')
SMG.DoClick = function()
	RunConsoleCommand("giveme 2")
end

end)
[/lua]

Isnt ‘;’ being used for in C# and other similar languages?

Also: Could you tell me where I made mistakes?

Yes, but it doesn’t make a difference when used in Lua. It’s just a habit of people when writing Lua.

Guess my Edit was quite late. So What Exactly did I do wrong? I read the code and the only things I seem to find I did wrong was that I negolected tostring.
And the other part: I think I could of done it in my way



usermessage.Hook("psay_command",ShowGui)


right?

And I dont think we need to specify all arguments or?

; is generally used in most other languages, however you can use them in LUA, being a C++ programmer I made a habit of using them.

Ok but…Can you please tell me what i’ve done wrong and why my above code wouldn’t work?

That’s just not how it works?



usermessage.Hook("psay_command",ShowGui)


Is this possible instead of



usermessage.Hook("psay_command",function(blabla) blblabal end)


?

yes

Ok but, why EXACTLY didnt my usermessage work?

Have you actually sent the clientside file to the client with **[G.AddCSLuaFile

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=G.AddCSLuaFile)** in a serverside file?

Micko9, don’t use semicolons please, they’re a general nuisance for people who want to code in lua. If you’re coding in another language why use the wrong syntax…?
Another thing is use more meaningful variable names, and finally tab properly. I can’t stress the importance of being a code neatfreak, it sounds like it takes more time initially but in the end it saves you a lot of mindfuck and more importantly, actually makes the whole process much quicker.
So say, instead of using “DFrame1” use something like “GunFrame”

his variables are local it doesn’t matter what he names them…

I used a .NET program called DermaDesigner. It generated the code for me, I didn’t put those semis, and why even care about that?
One thing I didnt like is that these variables weren’t local. I changed them and that’s all that I needed to do.
I don’t see a reason why you are such a harsh person with code to a person who started out with lua.
Thank your for your post anyway.

@Leiftiger Wow thanks, never knew I actually had to do that.I thought placing it in autorun would of been enough.

I used to think that too until I had a look at fixing some stuff I made half a year back



function SWEP:rebound(hp,norm,from)
f=self:traceent(100,hp,norm)
self:dmgit(f)
rotang=Angle((norm):Angle())
fr=from
fr:Rotate(rotang)
//self:bullsmoke(hp,(hp+(fr*-1)*80))
self:bullsmoke(hp,hp+(norm*-1+fr)*-80)
print(from:Angle())
end

my eyes died that day, been doing this sort of thing for like, 3 years on roblox yeah you heard me I said roblox and another 6 months on gmod before I realised how careless I’d been

This has got to be the dumbest thing i’ve ever seen. Semi-colons are a good practice, and makes your code look prettier. It’s not wrong syntax, it’s just unnecessary. In no way are they a “nuisance” they only make the flow much better.

This kid has no clue what he’s talking about.

Your code should work fine. Just add


AddCSLuaFile("autorun\client\cl_commands.lua")

to your sv_commands.lua file. This tells the server to send your file to the client, if you don’t do this the client wont have it and it wont work.

Also about semi-colons, they are acceptable in lua, but are not required. When you program in other languages, like c, they are required. Its not a big deal if you do or don’t here so why complain? It makes it easier for c and other users to jump into programming lua without needing to adjust to it too much.