So basically I am attempting to create a board where a user later can buy a position and give that position an image. The issue is that I use two positions of the table (Primary / Secondary -Banner)
and at the restart of the id-table, the primary is the same as the secondary even though the secondary is supposed to be always one higher than primary
Screenshot of output:
This isn’t supposed to happen as this will switch two times in a row through the same banner.
I myself cannot identify why this problem is occurring
The loop:
while true do
local choosen = {}
Primary_StartPos += 1
local Secondary_StartPos = Primary_StartPos + 1
if Primary_StartPos > #test_ids then
Primary_StartPos = 1
end
if Secondary_StartPos > #test_ids then
Secondary_StartPos = 1
end
choosen[1] = test_ids[Primary_StartPos]
choosen[2] = test_ids[Secondary_StartPos]
warn("Primary Banner: ".. tostring(Primary_StartPos) .. " | Secondary Banner: ".. tostring(Secondary_StartPos))
One.Image = choosen[1].Image
Two.Image = choosen[2].Image
local T1 = TweenService:Create(One,NewTweenInfo,{Position=UDim2.new(-0.5,0,0.5,0)})
local T2 = TweenService:Create(Two,NewTweenInfo,{Position=UDim2.new(0.5,0,0.5,0)})
T2:Play()
T1:Play()
T2.Completed:Connect(function()
One.Image = choosen[2].Image
One.Position = UDim2.new(0.5,0,0.5,0)
Two.Image = choosen[1].Image
Two.Position = UDim2.new(1.5,0,0.5,0)
end)
wait(SwitchTime * choosen[1].Package)
end
I see why this issue occurs, when Primary_StartPos reaches 5, the Secondary_StartPos becomes 6 and is over 4 items which causes the seondary one to become 1 & start pos is also over 4, it would also reset to 1 making it give you Secondary_StartPos = 1 & Primary_StartPos = 1
What i recommend is using a mod operator aka the % symbol. This is just like dividing except it gives you the remainder of the numerator over the denominator. This is useful since you can make the two variables start over and over but just increasing the value. An example how you can do this is
local newPrimary_StartPos = (Primary_StartPos % #test_ids) + 1
This will make a new variable that would be the value thats going to loop over from 1 to the max of test ids. Example Values Primary_StartPos → Returns
0 → 1
1 → 2
2 → 3
3 → 4
4 → 1
…
Except the Primary_StartPos must start at 0 for it to become 1 as you can see above, so you can update the values at the end of the code, and not the start. For Secondary_StartPos, its going to be the same thing, but have the local Secondary_StartPos = Primary_StartPos + 1 left in there since you wanted the secondary one to be 1 more then the primary one
After all of that you should have these two variables that you can use instead:
local newPrimary_StartPos = (Primary_StartPos % #test_ids) + 1
local newSecondary_StartPos = (Secondary_StartPos % #test_ids) + 1