Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Table inserting issue? [SOLVED, silly error in code]

simple code, have a blank table with part CFrames in numberical order. Yet when I print the values, it seems table[1] is part 3, table[2] is 1, table[3] is 4, and table[4] is 2. Is this a bug or am I just doing something wrong...

table.insert(pointsTable,1,pointsModel[1].CFrame)
table.insert(pointsTable,2,pointsModel[2].CFrame)
table.insert(pointsTable,3,pointsModel[3].CFrame)
table.insert(pointsTable,4,pointsModel[4].CFrame)

EDIT: Tested this by changing it to

for i = 1, 4 do
    pointsTable[i] = "pointsModel"..i
end

It seems to work fine with strings, but not with CFrame

SECOND EDIT:

I had a line before that sets pointsModel =workspace.Points:GetChildren(), forgetting that makes a table in a random order.

1 answer

✓ Accepted answer

The second argument to table.insert is optional. If you want to insert the new entry at the end, just remove that argument:

for i = 1, 4 do
	table.insert(pointsTable, pointsModel[i].CFrame)
end

Alternatively, just directly set the Table's members:

for i = 1, 4 do
	pointsTable[i] = pointsModel[i].CFrame
end

2 comments

0

Didn't work. I tried both methods and it still has the same order as 3,1,4,2

0

nvm, I figured it out. Thanks for the simplified code though, lol.

This discussion is preserved as it was. New answers, comments, and votes are closed.