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

How to get GUI from mouse button 1 down?

Long story short, I'm trying to make a color selector thing and I'm trying to make it without having to put a script in every frame, but what I have bellow wont work because I cant get the GUI from MouseButton1Down. How do you fix?

local kids = script.Parent:GetChildren()
function color(click)
	print(click.BackgroundColor3)
end
for i=1,#kids do
	if kids[i].ClassName == "Frame" and kids[i]:FindFirstChild("TextButton")~=nil then
		local fun = kids[i].TextButton.MouseButton1Click:connect(color)
		wait()
	end
end

1 answer

✓ Accepted answer
--First off, the way you iterate throught the children is
--acceptable, but I highly suggest learning pairs. It looks
--something like this:

--for index, kid in pairs(script.Parent:GetChildren()) do
	--print(index..": "..kid.Name)
--end

--This replaced "i" in your function with "index" and "kids[i]"
--in your function with "kid". Looks a bit cleaner.

kids = script.Parent:GetChildren() --Get all the children in a table

for _, kid in pairs(kids) do --FOr every child
    if kid:IsA("Frame") and kid:FindFirstChild("TextButton") ~= nil then --If the child is a frame with a TextButton
	
		--Next, you have anonymous functions. These functions are made up
		--on the spot. You can connect the MouseButton1Down event to an
		--anonymous function in this scope to get which child was clicked.
		
        kid.TextButton.MouseButton1Down:connect(function() --Connect the MouseButton1Down event to an anonymous function
			print(kid.BackgroundColor3) --Print the child's BackgroundColor3
		end) --End anonymous function
    end--End if statement
end --End loop

1 comment

0

However... The way that the iteration was done is -faster-

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