I’m trying to make the BorderColor3 of a Frame change colour for each iteration of a for count loop. Any ideas?
What part are you stuck on? You need to provide more information for us to help you.
Do you want the colors to change randomly? In a rainbow pattern?
I think the solution to your question as is would be as simple as just setting the color to something different for every loop.
im trying to get the bordercolor to change to a color of my choice on each iteration. for example
iteration 1: red
2: green
3: blue
im using a touched event by the way
If you’re controlling it, why are you using a loop? Please describe what you’re trying to do in more detail, a picture would be helpful. I’m not sure how you could control what color the border would be on just a touched event alone, you would need some other buttons or GUI to dictate it, unless you wanted it to cycle through colors.
Yes, cycle through colours, thats the effect im going for
I’m using a loop because the transparency of the part which the frame is parented under is changing with each iteration
If you want it to cycle then use an if/then statement to check what color it is already and to decide next.
if Box.BorderColor3 == Green then -- example I know that's not a color code
Box.BorderColor3 == Blue
elseif Box.BorderColor3 == Blue then
Box.BorderColor3 == Red
-- and so on...
If you’re going for a continuous for loop that’d iterate through the colors over and over, you can put them in a queue.
As in, you’ll declare a table and put your 3 colors into it, and you’ll initialize your for loop. Inside of it you’ll use the remove function on the table library to remove the first element and get it, then you’ll use it to set the color of the frame and then you’ll use the insert function to put it at the end of your table. And add any sort of waiting or halting of your choice between each iteration, that’s dynamic to your code.
Example:
local colors = { Color1, Color2, Color3 } --Replace the example elements with the color codes you want (or any element in the long run)
for i= 0, 1000 do --Change the 1000 to your amount of iterations
local color = table.remove(colors, 1) --Removing the first element and returning it, assigning it to color variable
-- Insert any code you want here
table.insert(colors, color) --Inserting the color again at the end so the iteration goes on and on
end
Why are you removing and adding? You could just use ‘i’ as the index.
local colors = { Color1, Color2, Color3 }
for i = 0, 1000 do -- Change the 1000 to your amount of iterations
local color = colors[i]
-- Do stuff
end
If the iteration is intended to happen between the colors only once, then yes, we can do it like this:
for i = 1, 3 do -- 0, 1000 won't work since it'll produce nil on 0 and from 4 to 1000
local color = colors[i]
-- Do stuff
end
However, for a continuous loop that’s intended to keep repeating for an amount of time, repeating the switching of colors (which is what my post was for) then a queue using table.remove and table.insert is a good option.
A more efficient way would be:
for i = 1, 1000 do
local color = colors[(i % #colors) + 1]
end
Using the modulo operator you can loop over the set of colors while constantly increasing the index you calculate from, you need to add 1 to it though since Lua arrays start at 1.
Operations like table insert and remove are very CPU intensive since it requires the entire array to be shifted. Which isn’t a problem when you only do it occasionally but in a constant loop it’s a waste of resources.
That’s in fact another good and efficient way to go about it, especially in larger arrays where stacking/queuing would get expensive for the CPU. Queuing can be more convenient at times but slightly less efficient.
Could be a typo, but the loop in your code should have a starting index of 0 not 1, since you’re adding 1 in the array index anyway, or it’ll skip the first element.