Multiple functions under the same DoClick button....

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


                     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

I’m sure its a syntax issue like that…any advice?

the “do” is only for loops, remove it.

Removed it but the entire script won’t even load but I can’t find error messages to start down issue…

Let’s start by fixing the indentation and that if-then syntax



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


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.



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


[del]This will probably work[/del], but it can be done better.



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


Now it uses a table to look up the appropriate string to send to clients. BUT WAIT! We aren’t done yet



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


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:

How do you even fire the DoClick event of a button on the server’s side?

Why do you always use “DO” followed by parentheses after a statement?


                     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

EDIT:

Ninja’d x2

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.

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]

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]

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

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.



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


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



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)


Server



-- 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)


I wrote this in a hurry, so there may be something wrong with it still.

This is for a tech demo. Very specialized situation. I’m not concerned with security whatsoever. It’s for demonstration purposes.

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.

What you did worked great, thank you so much! :happy:

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 :stuck_out_tongue: