• How to run a function in another file
    4 replies, posted
Hello, before you guys start judging me on how "noobish" of a question this is note that I am new to lua and I don't really understand the whole concept of it. But I am getting there, so bare with me for now. The problem that I am having is that the function isn't being triggered when ran from another file. I get a script error in console stating: [code] [ERROR] LuaCmd:1: attempt to call global 'sm_openMenu' (a nil value) 1. unknown - LuaCmd:1 [/code] Now I am sure that this problem is caused because the file where the function is isn't being included within the file that is trying to run it. Here are my other scripts: client: [code] function sm_OpenMenu() local serverMenu = vgui.Create( "DFrame" ) serverMenu:SetPos( 50, 50 ) serverMenu:SetSize( 100, 200 ) serverMenu:SetTitle( sm_menuTitle ) serverMenu:SetVisible( true ) serverMenu:SetDraggable( true ) serverMenu:ShowCloseButton( true ) serverMenu:MakePopup() end [/code] shared: [code] if ( SERVER ) then sm_openCMD = "servers" --Command to open menu //Ignore, it isn't being use atm sm_menuTitle= "Servers Menu" --Title of menu include( "sv_servermenu.lua" ) else include( "cl_servermenu.lua" ) end [/code] server: [code] hook.Add( "PlayerSay", "Chatcmd", function( ply, text, pblc ) if string.lower( text ) == "!servers" then ply:SendLua("sm_openMenu()") return "" // Deletes text from chat end end ) [/code]
Lua is a case sensitive language, the function name as an uppercase O, what you are calling doesn't. Instead of sending lua via ply:SendLua, it's generally better regarded to send a net message instead. Also, Lua in Garrysmod exists in 2 realms: Server and Client (there's a 3rd one called Menu but there isn't much you can do in there thats useful). These realms are seperate, so a global variable in 1 cannot be set from the other. You are setting sm_menuTitle on the server, but using it on the client. That won't work. Shared isn't actually a realm, it just means that the code is run in Both. The previous rule still applies, setting something on the server in a shared file will not set it on the client. This is why net messages are awesome, you could send the DFrame title via a netmessage, and set it that way. Net messages are useful for triggering events (like opening this DFrame), and sending data to the client (like setting the DFrame's title). This is why I advise you look up this page: [url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url]
[QUOTE=James xX;48026427]-sip-[/QUOTE] Thanks for the help I completely forgot about net Edit: Huh, for some reason the client side isn't receiving the net message. I am sure that this is my fault though. Here is my script: client: [code] if CLIENT then net.Receive( "SM_OPENMENU", function() print( "RECEIVED" ) local serverMenu = vgui.Create( "DFrame" ) serverMenu:SetPos( 50, 50 ) serverMenu:SetSize( 100, 200 ) serverMenu:SetTitle( sm_menuTitle ) serverMenu:SetVisible( true ) serverMenu:SetDraggable( true ) serverMenu:ShowCloseButton( true ) serverMenu:MakePopup() end ) end [/code] server: [code] if SERVER then util.AddNetworkString( "SM_OPENMENU" ) hook.Add( "PlayerSay", "Chatcmd", function( ply, text, pblc ) if string.lower( text ) == "!servers" then print( "SEND" ) net.Start( "SM_OPENMENU" ) net.Send( ply ) return "" end end ) end [/code] I added print in to test to see if the client-side was receiving what the server sent. Only SEND is printed in console.
Works for me, are you sure the clientside file is being included?
[QUOTE=James xX;48027054]Works for me, are you sure the clientside file is being included?[/QUOTE] Forget what I said above. It was just a problem on my part. Thanks for the help Solved.
Sorry, you need to Log In to post a reply to this thread.