Server chat commands not working when multiple functions in file
13 replies, posted
I'm having a problem where whenever I join my server (ran on my PC) I enter one of the chat commands that I created and put in the server addons folder, and it works fine, but when I try another command it doesn't work. It doesn't matter which command I enter first, whichever command I DO enter first is the only one that works until I rejoin the server and type in the other, and the same thing happens
Here's what I have in the sv_customhud folder located in C:\gmod\garrysmod\addons\customhud\lua\autorun\server
[CODE]hook.Add("Initialize","customhud_opengui")
util.AddNetworkString("customhud_opengui")
function ChatCommand (ply, txt, pub)
if string.sub(string.lower (txt), 1, 3 ) == "!sm" then
net.Start ("customhud_opengui")
net.Send (ply)
return""
end
end
hook.Add("PlayerSay", "ChatCommand", ChatCommand)
hook.Add("Initialize","SoloHud_opengui")
util.AddNetworkString("SoloHud_opengui")
function ChatCommand ( ply, txt, pub )
if string.sub( string.lower( txt ), 1, 5 ) == "!test" then
net.Start ("SoloHud_opengui")
net.Send (ply)
return ""
end
end
hook.Add("PlayerSay", "ChatCommand", ChatCommand)[/CODE]
And this is in the cl_customhud located in C:\gmod\garrysmod\addons\customhud\lua\autorun\client
[CODE]hook.Add("Initialize","customhud_opengui")
function DrawGUI ()
local frame = vgui.Create("DFrame")
frame:SetPos( 25, 600 )
frame:SetSize(140, 80)
frame:SetTitle( "Say To Server" )
frame:MakePopup()
frame:ShowCloseButton ( false )
function frame:Paint( w, h )
draw.RoundedBox(4, 0, 0, w, h, Color( 10, 10, 10, 220 ) )
draw.RoundedBoxEx(4, 0, 0, w, 20, Color( 10, 149, 209, 220 ), true, true, false, false )
end
local TextEntry = vgui.Create( "DTextEntry", frame ) -- create the form as a child of frame
TextEntry:SetPos( 10, 40)
TextEntry:SetSize( 120, 20 )
TextEntry:SetText( "" )
TextEntry.OnEnter = function( self )
chat.AddText( self:GetValue() ) -- print the form's text as server text
frame:Close()
end
but_close = vgui.Create( "DButton", frame)
but_close:SetPos(110, 0)
but_close:SetSize(30, 20)
but_close:SetText("X")
but_close:SetTextColor( Color(255, 255, 255))
function but_close:Paint(w,h) end
but_close.DoClick = function()
frame:Close()
end
-- player_select = vgui.Create ("DComboBox", frame)
-- player_select:SetPos(10, 30)
-- player_select:SetSize( 120, 20)
-- for k, v in pairs ( player.GetAll() ) do
-- player_select:AddChoice( tostring( v:Name() ) )
-- end
-- lbl_label = vgui.Create ("DLabel", frame )
-- lbl_label:SetPos(10,15)
-- lbl_label:SetSize(120, 20)
-- lbl_label:SetTextColor( color_white )
-- lbl_label:SetText( "Select Player" )
end
net.Receive( "customhud_opengui", DrawGUI )
hook.Add("Initialize","SoloHud_opengui")
function DrawGUI()
local frame = vgui.Create ("DFrame")
frame:SetTitle("Information")
frame:SetPos(50, 50)
frame:SetSize( ScrW() - 100, ScrH() - 100 )
frame:MakePopup()
frame:ShowCloseButton(true)
function frame:Paint(w,h)
draw.RoundedBox(4, 0, 0, w, h, Color(10, 10, 10, 220) )
draw.RoundedBoxEx(4, 0, 0, w, 25, Color(240, 85, 0, 240), true, true, false, false)
end
local Avatar = vgui.Create( "AvatarImage", frame )
Avatar:SetSize( 128, 128 )
Avatar:SetPos(20, 40)
Avatar:SetPlayer( LocalPlayer(), 128 )
end
net.Receive( "SoloHud_opengui", DrawGUI )[/CODE]
I tried to fix this and haven't had any success please help, thanks!
Hooks need unique names
[QUOTE=NiandraLades;48038151]Hooks need unique names[/QUOTE]
So I should change where it says "hook.Add("Initialize","customhud_opengui")"
to something like "hook.Add("Initialize_two","customhud_opengui")?"
sorry I'm new to GMOD coding
[QUOTE=Genesis_;48038181]So I should change where it says "hook.Add("Initialize","customhud_opengui")"
to something like "hook.Add("Initialize_two","customhud_opengui")?"
sorry I'm new to GMOD coding[/QUOTE]
The order of arguments in [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/hook/Add]hook.Add[/url] is (Event, Identifier, Function). The identifier, the second argument, needs to be unique.
[QUOTE=AK to Spray;48038210]The order of arguments in [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/hook/Add]hook.Add[/url] is (Event, Identifier, Function). The identifier, the second argument, needs to be unique.[/QUOTE]
The identifier is "customhud_opengui" correct? If so, it is unique.
You're missing the function. hook.Add has to take THREE parameters.
[QUOTE=code_gs;48038255]You're missing the function. hook.Add has to take THREE parameters.[/QUOTE]
So this would be correct
[CODE]hook.Add("Initialize","SoloHud_opengui", DrawGUI)
function DrawGUI()
local info = vgui.Create ("DFrame")
info:SetTitle("Information")
info:SetPos(50, 50)
info:SetSize( ScrW() - 100, ScrH() - 100 )
info:MakePopup()
info:ShowCloseButton(true)
function info:Paint(w,h)
draw.RoundedBox(4, 0, 0, w, h, Color(10, 10, 10, 220) )
draw.RoundedBoxEx(4, 0, 0, w, 25, Color(240, 85, 0, 240), true, true, false, false)
end
local Avatar = vgui.Create( "AvatarImage", info )
Avatar:SetSize( 128, 128 )
Avatar:SetPos(20, 40)
Avatar:SetPlayer( LocalPlayer(), 128 )
end
net.Receive( "SoloHud_opengui", DrawGUI )[/CODE]
[editline]24th June 2015[/editline]
[QUOTE=code_gs;48038255]You're missing the function. hook.Add has to take THREE parameters.[/QUOTE]
Added the code in the last reply and still doesn't work, I also tried put the two different functions into a different folder/file and still doesn't work. Please help or elaborate what you were saying
Hooks go underneath the functions they use.
[editline]23rd June 2015[/editline]
Also, I don't think you're using the hook right; you shouldn't use a function with a hook and net.Receive.
[QUOTE=code_gs;48038398]Hooks go underneath the functions they use.
[editline]23rd June 2015[/editline]
Also, I don't think you're using the hook right; you shouldn't use a function with a hook and net.Receive.[/QUOTE]
If not how do I communicate with the server and client without net.Receive?
You can use net.Receive. I just don't know why you need the Init hook.
[QUOTE=code_gs;48038465]You can use net.Receive. I just don't know why you need the Init hook.[/QUOTE]
How else would I do it? I tried everything you said but it just made both HUD's pop up as soon as I join the server without typing the commands. Could I just completely remove the Init hook or not? Like I said, I'm new to lua.
Yes, you can.
[QUOTE=code_gs;48041145]Yes, you can.[/QUOTE]
Alright thanks, I'll let you know if it worked!
[QUOTE=code_gs;48041145]Yes, you can.[/QUOTE]
Still works without init hook, but I still have the problem to where only one command works. Now, though the command "!sm" doesn't work even if I enter it first, but "!info" works no matter what.
Sorry, you need to Log In to post a reply to this thread.