Instance Getting moved randomly in a table

I’m making a turn-based rpg system wherein the turns would alternate between players and enemies, to do this I have a function that would pre-load the turns into a table and would occasionally update it as the combat progresses

How it works:
In the table, I set the allies’ index as odd numbers and the enemies’ as even numbers:
image

It works fine for the first 4 items in the table but when it comes specifically at the fifth index, it somehow pushes the ally (which should be at number 5) to the next index which is 6, and when the next enemy is supposed to be placed at index 6, it would again move that ally to the next spot which is the 7th index, resulting to this output seen in the picture below:

image

This is weird because the calculations are correct and are printing the correct outputs:


This also shows that it was placed at the 5th index

image
then was randomly moved to the 6th index after Enemy 2 was added at the 4th index

This is really weird and I’ve tried to research about this but no results came up, I hope someone can help me with this

here’s the part of the code btw if you still need to review it:

local curIndex = 0
			for i, ally in pairs(BattleData.AlliesFolder:GetChildren()) do
				
				print(i + curIndex, ally)
				--table.insert(BattleData.TurnData["Turn".. BattleData.CurrentTurnIndex.Value], i + curIndex, ally)
				BattleData.TurnData["Turn".. BattleData.CurrentTurnIndex.Value][i + curIndex] = ally
				print(BattleData.TurnData["Turn".. BattleData.CurrentTurnIndex.Value])
				curIndex += 1
		
			end
			
			for i, ally in pairs(BattleData.CurrentEnemiesFolder:GetChildren()) do
				print(i * 2, ally)
				table.insert(BattleData.TurnData["Turn".. BattleData.CurrentTurnIndex.Value], i * 2, ally )
				print(BattleData.TurnData["Turn".. BattleData.CurrentTurnIndex.Value])
			end
1 Like

would it be possible to use ipairs instead of pairs? ipairs reads a table in a more orderly way, without randomizations as far as i know, but only works if its structured as an array (index are numbers), which it appears your table is

Mmm, alright I can try. I’ll update you about the results later!!!