Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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

Log in to vote
6
Answered by 8 years ago

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

Ad

Answer this question