• Multiple functions under the same DoClick button....
    15 replies, posted
I'm still learning... I'm trying to get the button to do a different function each time (progressive)...I only need 3 times really [CODE] button2pressed = 0 Button2.DoClick = function () if button2pressed = 0 then do (BroadcastLua(surface.PlaySound("sound1.wav"))) print ("button pressed once") button2pressed = 1 elseif (button2pressed == 1) then do (BroadcastLua(surface.PlaySound("sound2.wav"))) print ("button pressed twice") button2pressed = 2 else do (BroadcastLua(surface.PlaySound("sound3.wav"))) print "Button pressed 3 times, resetting" button2pressed = 0 end end[/CODE] I'm sure its a syntax issue like that...any advice?
the "do" is only for loops, remove it.
[QUOTE=whitestar;48540036]the "do" is only for loops, remove it.[/QUOTE] Removed it but the entire script won't even load but I can't find error messages to start down issue...
[QUOTE=Blakestr;48539905]I'm still learning... I'm trying to get the button to do a different function each time (progressive)...I only need 3 times really [CODE] button2pressed = 0 Button2.DoClick = function () if button2pressed = 0 then do (BroadcastLua(surface.PlaySound("sound1.wav"))) print ("button pressed once") button2pressed = 1 elseif (button2pressed == 1) then do (BroadcastLua(surface.PlaySound("sound2.wav"))) print ("button pressed twice") button2pressed = 2 else do (BroadcastLua(surface.PlaySound("sound3.wav"))) print "Button pressed 3 times, resetting" button2pressed = 0 end end[/CODE] I'm sure its a syntax issue like that...any advice?[/QUOTE] Let's start by fixing the indentation and that if-then syntax [code] button2pressed = 0 Button2.DoClick = function () if button2pressed = 0 then BroadcastLua(surface.PlaySound("sound1.wav")) print ("button pressed once") button2pressed = 1 elseif button2pressed == 1 then BroadcastLua(surface.PlaySound("sound2.wav")) print ("button pressed twice") button2pressed = 2 else BroadcastLua(surface.PlaySound("sound3.wav")) print "Button pressed 3 times, resetting" button2pressed = 0 end end [/code] Now, BroadcastLua accepts a string, and sends that string to clients to be interpreted as Lua code. However, you have not wrapped it up into a string. It will instead attempt to run the code on the server (which won't work) and send the result to clients. [CODE] button2pressed = 0 Button2.DoClick = function () if button2pressed = 0 then BroadcastLua('surface.PlaySound("sound1.wav")') print ("button pressed once") button2pressed = 1 elseif button2pressed == 1 then BroadcastLua('surface.PlaySound("sound2.wav")') print ("button pressed twice") button2pressed = 2 else BroadcastLua('surface.PlaySound("sound3.wav")') print "Button pressed 3 times, resetting" button2pressed = 0 end end [/CODE] [del]This will probably work[/del], but it can be done better. [code] button2pressed = 1 local soundlist = { 'surface.PlaySound("sound1.wav")', 'surface.PlaySound("sound2.wav")', 'surface.PlaySound("sound3.wav")' } Button2.DoClick = function () BroadcastLua(soundlist[button2pressed]) button2pressed = button2pressed + 1 if button2pressed > 3 then button2pressed = 1 end end [/code] Now it uses a table to look up the appropriate string to send to clients. BUT WAIT! We aren't done yet [code] Button2.pressed = 1 local soundlist = { 'surface.PlaySound("sound1.wav")', 'surface.PlaySound("sound2.wav")', 'surface.PlaySound("sound3.wav")' } function Button2:DoClick() BroadcastLua(soundlist[self.pressed]) self.pressed = self.pressed + 1 if self.pressed > 3 then self.pressed = 1 end end [/code] Now it's more object oriented. I could keep going, but I think I'll stop here. It still needs more networking.
its because you set button2pressed always to 0 upon pressing, define it as a local var outside the doclick. [editline]25th August 2015[/editline] Ninja'd :v:
[QUOTE=Jcw87;48540081][...] Now, BroadcastLua accepts a string, and sends that string to clients to be interpreted as Lua code. However, you have not wrapped it up into a string. It will instead attempt to run the code on the server (which won't work) and send the result to clients. [...][/QUOTE] How do you even fire the DoClick event of a button on the server's side?
[QUOTE=Blakestr;48539905] [CODE] button2pressed = 0 Button2.DoClick = function () if button2pressed = 0 then do (BroadcastLua(surface.PlaySound("sound1.wav"))) print ("button pressed once") button2pressed = 1 elseif (button2pressed == 1) then do (BroadcastLua(surface.PlaySound("sound2.wav"))) print ("button pressed twice") button2pressed = 2 else do (BroadcastLua(surface.PlaySound("sound3.wav"))) print "Button pressed 3 times, resetting" button2pressed = 0 end end[/CODE][/QUOTE] Why do you always use "DO" followed by parentheses after a statement? [CODE] button2pressed = 0 Button2.DoClick = function () -- if button2pressed = 0 then do (BroadcastLua(surface.PlaySound("sound1.wav"))) NO! And why do you use BroadcastLua? It's for serverside. You are already in clientside. surface.PlaySound("sound1.wav") print ("button pressed once") button2pressed = 1 elseif (button2pressed == 1) then surface.PlaySound("sound2.wav") print ("button pressed twice") elseif (button2pressed == 2) then surface.PlaySound("sound3.wav"))) print "Button pressed 3 times, resetting" button2pressed = 0 end end[/CODE] EDIT: Ninja'd x2
[QUOTE=whisperity;48540104]How do you even fire the DoClick event of a button on the server's side?[/QUOTE] Very true, wasn't even thinking about that. Got caught up in fixing the bad syntax. So what you will actually want to do, is use a console command or a net message to send it to the server, and have the server broadcast it to clients.
[QUOTE=Jcw87;48540140]Very true, wasn't even thinking about that. Got caught up in fixing the bad syntax. So what you will actually want to do, is use a console command or a net message to send it to the server, and have the server broadcast it to clients.[/QUOTE] I tried your changes and it didn't work. No error messages. Not sure where to go with...I really appreciate the help everyone so far though [editline]25th August 2015[/editline] [QUOTE=Gedo789;48540135]Why do you always use "DO" followed by parentheses after a statement? [CODE] button2pressed = 0 Button2.DoClick = function () -- if button2pressed = 0 then do (BroadcastLua(surface.PlaySound("sound1.wav"))) NO! And why do you use BroadcastLua? It's for serverside. You are already in clientside. surface.PlaySound("sound1.wav") print ("button pressed once") button2pressed = 1 elseif (button2pressed == 1) then surface.PlaySound("sound2.wav") print ("button pressed twice") elseif (button2pressed == 2) then surface.PlaySound("sound3.wav"))) print "Button pressed 3 times, resetting" button2pressed = 0 end end[/CODE] EDIT: Ninja'd x2[/QUOTE] Broadcast was because I want every player to hear it...this is a special situation involving only 2 players who are on the same network [editline]25th August 2015[/editline] [QUOTE=Gedo789;48540135]Why do you always use "DO" followed by parentheses after a statement? [CODE] button2pressed = 0 Button2.DoClick = function () -- if button2pressed = 0 then do (BroadcastLua(surface.PlaySound("sound1.wav"))) NO! And why do you use BroadcastLua? It's for serverside. You are already in clientside. surface.PlaySound("sound1.wav") print ("button pressed once") button2pressed = 1 elseif (button2pressed == 1) then surface.PlaySound("sound2.wav") print ("button pressed twice") elseif (button2pressed == 2) then surface.PlaySound("sound3.wav"))) print "Button pressed 3 times, resetting" button2pressed = 0 end end[/CODE] EDIT: Ninja'd x2[/QUOTE] Broadcast was because I want every player to hear it...this is a special situation involving only 2 players who are on the same network. I tried using your corrected code but it didn't work / no errors...i'll try reloading to see if that makes a difference
-snip- Explain what exactly you want. BroadcastLua doesn't seem ideal here, and usually never is anywhere. By the look of it you'd be better off using net messages and net.Broadcast
[QUOTE=YourStalker;48540359]-snip- Explain what exactly you want. BroadcastLua doesn't seem ideal here, and usually never is anywhere. By the look of it you'd be better off using net messages and net.Broadcast[/QUOTE] This is a special situation. I need that sound heard by everyone on the server. I am not worried about latency or security whatsoever. BroadcastLua seems to be the simplist but I can't get this If statement to work...
Ok, I have a little more time now. Let's start with where I left off. [CODE] Button2.pressed = 1 local soundlist = { 'surface.PlaySound("sound1.wav")', 'surface.PlaySound("sound2.wav")', 'surface.PlaySound("sound3.wav")' } function Button2:DoClick() BroadcastLua(soundlist[self.pressed]) self.pressed = self.pressed + 1 if self.pressed > 3 then self.pressed = 1 end end [/CODE] So, you need to understand how the Lua states in Garry's Mod works. There are 2 separate Lua environments, the server, and the client. The server runs the server Lua state (obviously) and every client has its own client state. To get information from one state to another, you have to use networking. In this case, you have a Derma menu that you want to have play a sound for all clients. That means you have to tell the server about it, and then the server has to tell all the clients about it. Furthermore, you will want some kind of authentication on the server, so that some random jackass doesn't start abusing your sound player. Client [CODE] Button2.pressed = 1 local soundlist = { "sound1.wav", "sound2.wav", "sound3.wav" } function Button2:DoClick() -- Start a network message with a name. -- This name will be used on the server to call the correct handling code. net.Start("MyRequestSound") -- Write the name of the sound you want to play. net.WriteString(soundlist[self.pressed])) -- Send it to the server net.SendToServer() self.pressed = self.pressed + 1 if self.pressed > 3 then self.pressed = 1 end end -- After the server receives your request to play the sound, it will tell clients to play it. -- Handle that here net.Receive("MyPlaySound", function(len) local soundname = net.ReadString() surface.PlaySound(soundname) end) [/CODE] Server [CODE] -- The server needs to know about all net message names before they are used util.AddNetworkString("MyRequestSound") util.AddNetworkString("MyPlaySound") net.Receive("MyRequestSound", function(len, ply) -- make sure that the player requesting the sound isn't some random jackass if not ply:IsAdmin() then return end -- Read the sound name sent from the client local soundname = net.ReadString() -- Start a net message net.Start("MyPlaySound") -- Write the sound name net.WriteString(soundname) -- Send it to all clients net.Broadcast() end) [/CODE] I wrote this in a hurry, so there may be something wrong with it still.
[QUOTE=Jcw87;48540700] I wrote this in a hurry, so there may be something wrong with it still.[/QUOTE] This is for a tech demo. Very specialized situation. I'm not concerned with security whatsoever. It's for demonstration purposes.
[QUOTE=Blakestr;48540736]This is for a tech demo. Very specialized situation. I'm not concerned with security whatsoever. It's for demonstration purposes.[/QUOTE] Ok, thats nice. If you really don't want security, you can just comment out the 1 line partaining to that. Everything else I just posted is still relevant to what you want.
[QUOTE=Jcw87;48540955]Ok, thats nice. If you really don't want security, you can just comment out the 1 line partaining to that. Everything else I just posted is still relevant to what you want.[/QUOTE] What you did worked great, thank you so much! :happy:
[QUOTE=Blakestr;48540596]I am not worried about security whatsoever. BroadcastLua seems to be the simplist but I can't get this If statement to work...[/QUOTE] Then you come on to the Next Update thread a months later to complain how you're being hacked or you spend money for someone to fix it. You ended up doing it my way anyways :P
Sorry, you need to Log In to post a reply to this thread.