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

Anyone know of a more efficient way for coding gui's being clicked?

Alright so say I've got this code:

local OBJGui = player.PlayerGui.CatagoryGui.Frame

OBJGui.ObjectOne.MouseButton1Click:connect(function()
	object = OBJGui.ObjectOne.Text
end)

OBJGui.ObjectTwo.MouseButton1Click:connect(function()
	object = OBJGui.ObjectTwo.Text
end)

OBJGui.ObjectThree.MouseButton1Click:connect(function()
	object = OBJGui.ObjectThree.Text
end)

OBJGui.ObjectFour.MouseButton1Click:connect(function()
	object = OBJGui.ObjectFour.Text
end

Is there any way to do this more efficiently instead of making countless amounts of _______.MouseButton1Click:connect(function()

This just seems horribly inefficient to me considering things like shop guis. I've tried to google it, but found nothing . Thanks for the help.

1 answer

Yes, there is a way!


First, you want to change the numbers in the names of objects to actual numbers (not words). E.G, Object1, Object2, Object3

Next, you want to loop through all of the objects using a number, then work from there, like so:

local OBJGui = player.PlayerGui.CatagoryGui.Frame
local object -- i defined this earlier so it's more efficient

for i = 1,#OBJGui:GetChildren() do -- how many objects are inside the object holder
	local obj = OBJGui:FindFirstChild("Object"..i) -- Object1, Object2, Object3, etc.
	if obj then -- if it exists
		obj.MouseButton1Down:connect(function()
			object = obj.Text
		end)
	end
end

Hope I helped!

~TDP

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