Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a better way to script MouseButton1Click?

Currently I have this but I feel like this is terribly inefficient, and makes it very hard on me when adding, in this case, colors.

BLKGui.Blue.MouseButton1Click:connect(function()
	newblock.BrickColor = BrickColor.new("Really blue")
	colorselect()
	color = "Really blue"
	wait()
	BLKGui.Blue.Image = "rbxgameasset://Images/blueS"
end)

BLKGui.Black.MouseButton1Click:connect(function()
	newblock.BrickColor = BrickColor.new("Black")
	colorselect()
	color = "Black"
	wait()
	BLKGui.Black.Image = "rbxgameasset://Images/blackS"
end)

BLKGui.Green.MouseButton1Click:connect(function()
	newblock.BrickColor = BrickColor.new("Lime green")
	colorselect()
	color = "Lime green"
	wait()
	BLKGui.Green.Image = "rbxgameasset://Images/greenS"
end)

BLKGui.Red.MouseButton1Click:connect(function()
	newblock.BrickColor = BrickColor.new("Really red")
	colorselect()
	color = "Really red"
	wait()
	BLKGui.Red.Image = "rbxgameasset://Images/redS"
end)

BLKGui.White.MouseButton1Click:connect(function()
	newblock.BrickColor = BrickColor.new("Lily white")
	colorselect()
	color = "Lily white"
	wait()
	BLKGui.White.Image = "rbxgameasset://Images/whiteS"
end)

BLKGui.Yellow.MouseButton1Click:connect(function()
	newblock.BrickColor = BrickColor.new("Bright yellow")
	colorselect()
	color = "Bright yellow"
	wait()
	BLKGui.Yellow.Image = "rbxgameasset://Images/yellowS"
	
end)

basically if I could do this:

GuiName.MouseButton1Click:connect(function(objclicked)
	-- code with objclicked
end)

It would make my life a lot easier. Is this a thing, or do I HAVE to do it like it is in the top script.

2 answers

✓ Accepted answer

#What you could do Well remastered box isn't wrong, you can use Tables and loops.


#Tables/Arrays These are variables that have multiple values inside of them, and they're written in this form: Table = {5/2,true,BrickColor.new(1)}. You address the value inside the table like so: Table[1] if we were still using the first table it would give you 2.5. Now by default you address each value by using numbers, but you can set each iterator. For example: Table = {a = "Hello", 4 = "LOL"}. This is called a Dictionary where each iterator is defined by a string in this example.

Another thing you can do with Tables is have tables inside of tables... Table = {1,{3,2},3,"Table-seption"}


#For Loop We're talking about a for loop that actually uses an iterator also, which is the "i" most people use, even though it can be anything most people, myself included, also use i to mean iterator...

for i = 1,10 do --i for Iterator; code will repeat 10 times.
--CODE
end

You put the code in the middle and it will run the chunk of code within it for however many times. Here's a diagram to maybe help you understand it better. What we can do is use the function pairs. In the example, i the iterator is labeled 1-4, but later in the table it says c = 12, c becomes the new iterator so that's why it prints

c 12

Table = {1,2,3,true,workspace.Part,412+32/2} --Another example, yes you can put full math equations in here...
for i,v in pairs(Table) do
     print(i,v) --Print the iterator and the value
end

#Final Code I used 2 for loops and a single dictionary.

local Buttons = {"Blue" = {"rbxgameasset://Images/blueS", "Really blue"},"Black" = {"rbxgameasset://Images/blackS","Black"},"Green" = {"rbxgameasset://Images/greenS","Lime green"},"Red" = {"rbxgameasset://Images/redS", "Really red"}, "White" = {"rbxgameasset://Images/whiteS","Lily white"}, "Yellow" = {"rbxgameasset://Images/yellowS", "Bright yellow"}}

for _,v in pairs(BLKGui:GetChildren()) do --We don't really need i so replace it with _, it's a habit; GetChildren returns a function
     if v:IsA("GuiButton") then --If the v is a GuiButton of any type(ImageButton or TextButton)
          v.MouseButton1Click:connect(function()
		newblock.BrickColor = BrickColor.new(Buttons[v.Name][2])
 	        colorselect()
    	        color = Buttons[v.Name][2]
   		wait()
   		v.Image = Buttons[v.Name][1]
	end)
     end
end


Hope it helps!

2 comments

0

exactly what I was looking for tyvm!

0

Haha you for outdid my answer!

This is easily accomplishable with a comb of the following:

http://wiki.roblox.com/index.php?title=Table http://wiki.roblox.com/?title=Loops

4 comments

0

could you give me some example code?

0

Sorry I was a little too late to reply :P

0

I'm just a tryhard like that :3

0

Oh yea, I feel you there. Look at most of my answers- I believe I only forgot to comment one or two. In fact, I'm going back through! ;3

This discussion is preserved as it was. New answers, comments, and votes are closed.