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

I am trying to do a very strange match up between variables in a loop, may i have help?

Asked by 6 years ago
Edited 6 years ago

Lets say for example I have a frame and in that frame is a bunch of ImageButtons and in those ImageButtons are a bunch of Values and if those values were different like 1,2,3,4,5,6 how would i create a loop to find 1,2 or 3,4 or 5,6? (In the scrolling frame, there are buttons and in those buttons are values like 1,2,3,4,5)

Any Help? This is the script i created:

(NewCounter is the incrementing value)

        repeat wait()
        for _, v in pairs(script.Parent.Parent.Parent.ScrollingFrame:GetChildren(firstValue, secondValue)) do
            if v:FindFirstChild("NewCounter") then
                print("Passed 1")
                 NewCounter = v:FindFirstChild("NewCounter")
                 OtherCounter = v:FindFirstChild("NewCounter")
                if NewCounter.Value == firstValue then
                    print("Passed 2")
                    firstValue = firstValue + 1
                    secondValue =secondValue + 1
                    print(OtherCounter.Value)
                    if OtherCounter.Value == secondValue then
                        print("Passed 3")
                        NewCounter.Value = OtherCounter.Value
                        NewCounter.Parent.Position = OtherCounter.Parent.Position
                        print("Weird script...")
                    end
                end
            end
        end
        until OtherCounter.Value == secondValue

Any help would be great, tell me if I need to add something more or if something is wrong with this question...

1 answer

Log in to vote
2
Answered by 6 years ago

It's a bit ambiguous what you want, but here's a few options:

local sf = script.Parent.Parent.Parent.ScrollingFrame
--Get children from firstValue to secondValue
local newCounter
local imageButtons = {}
for _, v in ipairs(sf:GetChildren()) do
    newCounter = v:FindFirstChild("NewCounter")
    if newCounter and newCounter.Value >= firstValue and newCounter.Value <= secondValue then
        table.insert(imageButtons, v)
    end
end
--If you have named each ImageButton in a formulaic way, ex "Image1", "Image2", etc, you can just do this:
local imageButtons = {}
for i = firstValue, secondValue do
    table.insert(imageButtons, sf["Image" .. i]) -- assumes that the ImageButton will always exist
end
--If secondValue is always 1 greater than firstValue and you have "Image1", "Image2", etc:
local firstImage = sf["Image" .. firstValue]
local secondImage = sf["Image" .. secondValue]

--Say you don't have them named in any special way and you want to iterate over all pairs (ex 1,2 followed by 3,4 etc) so that you can apply your Position command:
--First, get all the image buttons into a dictionary/array where the key is newCounter.Value and the value is the image button
local imageButtons = {}
local ch = sf:GetChildren()
local newCounter
local max = 0 -- if you can guarantee that there won't be holes (ex you'll never have "1, 2, 5, 6" without a 3 or 4 anywhere), you don't need max and you can just use #imageButtons instead
for i = 1, #ch do
    newCounter = ch[i]:FindFirstChild("NewCounter")
    if newCounter then
        imageButtons[newCounter.Value] = ch[i]
        if newCounter.Value > max then
            max = newCounter.Value
        end
    end
end
--Now you can iterate over them easily:
for i = 1, max, 2 do
    imageButtons[i].Position = imageButtons[i + 1].Position
end
2
These seems about right, thank you.. greatneil80 2647 — 6y
1
ur face seems about right neil Gey4Jesus69 2705 — 5y
0
thx greatneil80 2647 — 4y
Ad

Answer this question