• if (string.sub... on client-side
    25 replies, posted
Hey! I'm trying to do this: [CODE]if (string.sub(text, 1, 5) == "!shop") then function displayshop() local frame = vgui.Create("Frame") frame:SetSize( 600, 350 ) frame:Center() frame:SetVisible( true ) frame:MakePopup() end end[/CODE] But it just comes up with this error: [CODE]... bad argument #1 to 'sub' (string expected, not nil)[/CODE] The error makes no sense, as off this runs fine in server side. BTW the command is both server and client side. Alternativly I could call it from server-side, but how?
The variable text is nil so it holds no value, presuming this is all your code? My best guess would be your're trying to get that Vgui to open when you type in the word "!shop". Try something like this [CODE] hook.Add("PlayerSay","WhateverMakesYouHappy",displayshop(ply,text)) [/CODE] [url]http://wiki.garrysmod.com/page/GM/PlayerSay[/url] Add it to the bottom of your code. Basically hooks into whenever the Player types something in chat, for example if the Player types "Hello" in chat. the variable text will now equal "Hello" Also this wont work Serverside as Vgui is client side, so you have to run it Clientside. EDIT: Dam forgot the PlayerSay hook was Serverside EDIT: You're going to have to check for the message serverside using the PlayerSay hook, then send something like a net message or umsg to the client to trigger the Vgui.
Well, by looking at what you posted you're not passing anything to the text variable, so it's returning as nil. By the way, you can't call this from Client-Side because you would need to use the PlayerSay hook which is serverside. [code] hook.Add("PlayerSay", "OpenShop", function(ply, text, public) if (string.sub(text, 1, 5) == "!shop") then local frame = vgui.Create("Frame") frame:SetSize( 600, 350 ) frame:Center() frame:SetVisible( true ) frame:MakePopup() end end) [/code]
[QUOTE]The variable text is nil so it holds no value, presuming this is all your code? My best guess would be your're trying to get that Vgui to open when you type in the word "!shop". Try something like this [CODE]hook.Add("PlayerSay","WhateverMakesYouHappy",displayshop(ply,text))[/CODE] [url]http://wiki.garrysmod.com/page/GM/PlayerSay[/url] Add it to the bottom of your code. Basically hooks into whenever the Player types something in chat, for example if the Player types "Hello" in chat. the variable text will now equal "Hello" Also this wont work Serverside as Vgui is client side, so you have to run it Clientside. EDIT: Dam forgot the PlayerSay hook was Serverside EDIT: You're going to have to check for the message serverside using the PlayerSay hook, then send something like a net message or umsg to the client to trigger the Vgui.[/QUOTE] I will check:) [QUOTE]Well, by looking at what you posted you're not passing anything to the text variable, so it's returning as nil. By the way, you can't call this from Client-Side because you would need to use the PlayerSay hook which is serverside. [CODE]hook.Add("PlayerSay", "OpenShop", function(ply, text, public) if (string.sub(text, 1, 5) == "!shop") then local frame = vgui.Create("Frame") frame:SetSize( 600, 350 ) frame:Center() frame:SetVisible( true ) frame:MakePopup() end end)[/CODE][/QUOTE] I can't create a frame on server-side.. EDIT: We are gonna have to check: [url]http://wiki.garrysmod.com/index.php?title=Category:Client_Hooks[/url]
[QUOTE=jonasrh;41799816] I can't create a frame on server-side..[/QUOTE] Obviously. You'll need to call the frame code clientside. Use a console command ( ply:ConCommand & concommand.Add ) or send a net message.
[QUOTE=Robotboy655;41799842]Obviously. You'll need to call the frame code clientside. Use a console command ( ply:ConCommand & concommand.Add ) or send a net message.[/QUOTE] How exactly? BTW you could use [url]http://wiki.garrysmod.com/page/GM/OnPlayerChat[/url]
Hook OnPlayerChat, check text, check if ply is LocalPlayer, then do your menu shit. Something like this: (client-side) [lua] if( !CLIENT ) then return; end hook.Add( "OnPlayerChat", "DisplayShop", function( ply, text ) if( ply == LocalPlayer() && text:Left( 5 ) == "!shop" ) then //Menu here end end ); [/lua]
[QUOTE=Greetings;41799888]Hook OnPlayerChat, check text, check if ply is LocalPlayer, then do your menu shit.[/QUOTE] Like this? [CODE]function PlayerSay( ply, strText ) if (string.sub(strText, 1, 5) == "!shop") then local frame = vgui.Create("Frame") frame:SetSize( 600, 350 ) frame:Center() frame:SetVisible( true ) frame:MakePopup() end end hook.Add( "PlayerSay", "OnPlayerChat ", OnPlayerChat )[/CODE] Doesn't work.
No it won't, the hooks is Serveside so will only work on the Server and therefore cant be run Clientside
So how would I? Like ulx has !menu which brings up a window
[QUOTE=jonasrh;41800095]So how would I? Like ulx has !menu which brings up a window[/QUOTE] Send something back to the client to open the menu. Someone mentioned it earlier, send a net msg or umsg or whatever the fuck they are in GM13 once the server receives the required chat thing from the player. I dunno if this is the best way of doing it, but it should work.
Here's one way of doing it: [lua] if SERVER then hook.Add("PlayerSay", "PlayerSay", function(ply, text) if (string.sub(text, 1, 5) == "!menu") then ply:ConCommand("myMenu") end end) else concommand.Add("myMenu", function(ply, cmd, args) local frame = vgui.Create("Frame") frame:SetSize(600, 350) frame:Center() frame:MakePopup() end) end [/lua]
Ill do something quickly, have no idea if it is the best way but here you go. [CODE] if SERVER then util.AddNetworkString("Shop") --I prefer net over umsg so yeah net it is hook.Add("PlayerSay","Whatever",function(ply,text,teamchat) if string.lower(text) == "!shop" then net.Start("Shop") net.Send(ply) end end) end if CLIENT then function displayshop() --Menu stuff you had end net.Receive("Shop",displayshop) end [/CODE] Think that should work roughly, untested and I don't know if thats the best way. EDIT And Ninja'd Thanks, didn't notice Fixed.
Yeh. How would it work? Server: [CODE]net.Start( "shopDisplay" ) net.Send( ply )[/CODE] Client: [CODE]net.Receive( "shopDisplay", shopDisplay() )[/CODE]
[QUOTE=jonasrh;41800245]Yeh. How would it work? Server: [CODE]net.Start( "shopDisplay" ) net.Send( ply )[/CODE] Client: [CODE]net.Receive( "shopDisplay", shopDisplay() )[/CODE][/QUOTE] His code is wrong by the way. [QUOTE]net.Receive( "shopDisplay", shopDisplay() )[/QUOTE] should be [QUOTE]net.Receive( "shopDisplay", shopDisplay)[/QUOTE]
shopDisplay() would be the function?
Yes, it is the function to open your menu clientside.
Doesn't seem to work... Client (whole file): [CODE]net.Receive( "shopDisplay", shopDisplay ) function shopDisplay( ) local frame = vgui.Create("Frame") frame:SetSize( 600, 350 ) frame:Center() frame:SetVisible( true ) frame:MakePopup() end[/CODE] Server: [CODE]function chatCommand( ply, text, public ) if (string.sub(text, 1, 8) == "!steamid") then ply:PrintMessage( HUD_PRINTTALK, ply:SteamID() ) end if (string.sub(text, 1, 5) == "!shop") then util.AddNetworkString("shopDisplay") net.Start( "shopDisplay" ) net.Send( ply ) print("Net Send") end end hook.Add( "PlayerSay", "chatCommand", chatCommand )[/CODE] No errors. SteamID works + Net Send shows up in console..
[lua] --lua/autorun if SERVER then AddCSLuaFile() util.AddNetworkString("shopDisplay") function chatCommand(ply, text, public) if (string.sub(text, 1, 8) == "!steamid") then ply:PrintMessage(HUD_PRINTTALK, ply:SteamID()) elseif (string.sub(text, 1, 5) == "!shop") then net.Start("shopDisplay") net.Send(ply) end end hook.Add("PlayerSay", "chatCommand", chatCommand) else function shopDisplay() local frame = vgui.Create("Frame") frame:SetSize(600, 350) frame:Center() frame:MakePopup() end net.Receive("shopDisplay", shopDisplay) end [/lua]
Try adding the util.AddNetworkString("shopDisplay") before the function, you have to call it a few seconds before you use it. Btw, sorry about my first post, completely stupid :|
I think its due to the fact that you are calling net.Receive before the function is created? Try moving it to the bottom of the function. EDIT Ignore I didn't read properly
Nope not working. No error. But after debugging, it does see the !shop, and prop tries to send it, but client doesn't get it somehow:/
-snip-
[QUOTE=jonasrh;41800602]Nope not working. No error. But after debugging, it does see the !shop, and prop tries to send it, but client doesn't get it somehow:/[/QUOTE] It's because "Frame" isn't a valid VGUI element, change that to "DFrame".
[QUOTE=brandonj4;41800680]It's because "Frame" isn't a valid VGUI element, change that to "DFrame".[/QUOTE] Did you get an error testing it? EDIT: Really? I missed that.. Jeez.. Well it worked:D Thx for your help:)
[QUOTE=jonasrh;41800764]Did you get an error testing it?[/QUOTE] No, it's just knowledge.
Sorry, you need to Log In to post a reply to this thread.