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

Why isn't this loop going in order?

So I have this code and what it should do is load the items in game.ServerStorage.DATA_STORAGE.lucas4114.PlacedItems , it works fine but doesn't do it in the right order, this was what it looked like when I first loaded: https://gyazo.com/9a655074fc075c29bb5f1deb92385b6a then I left and rejoined and it loaded in a different order: https://gyazo.com/e574d6149051c0a55b62ce7d1d2b2e09

local itemstoload = {}
						for i,v in pairs (game.ServerStorage.DATA_STORAGE:FindFirstChild(player.Name).PlacedItems:GetChildren()) do
							table.insert(itemstoload, v.PlaceInObby.Value, v.Name)
						end
						table.sort(itemstoload)
						for i,v in pairs(itemstoload) do
							local item = game.ServerStorage.ObbyPartStorage:FindFirstChild(v):Clone()
							script.Parent.ObbyParts.Value = script.Parent.ObbyParts.Value + 1
							item.Parent = script.Parent.ObbyPartStorage
							item:SetPrimaryPartCFrame(script.Parent.NextConnectionPart.Value.CFrame)
							item.PlaceInObby.Value = script.Parent.ObbyParts.Value
							script.Parent.NextConnectionPart.Value = item.EndPart
							script.Parent.LastPartPlaced.Value = item
						end

This is the full script that loads the items:

local db = false

script.Parent.ObbyModel.Door.DoorPart.Touched:connect(function(part)
	if db == false then
		db = true
		if script.Parent.OwnerName.Value == "Empty" then
			if game.Players:FindFirstChild(part.Parent.Name) then
				if part.Parent:FindFirstChild("Humanoid") then
					if part.Parent:FindFirstChild("Humanoid").Health > 0 then
						local player = game.Players:FindFirstChild(part.Parent.Name)
						player:FindFirstChild("PlayersObby").Value = script.Parent.Name
						script.Parent.NextConnectionPart.Value = script.Parent.ObbyStartPoint
						script.Parent.LastPartPlaced.Value = nil
						script.Parent.OwnerName.Value = player.Name
						script.Parent.OwnerPlayer.Value = player
						script.Parent.OwnerCharacter.Value = part.Parent
						script.Parent.ObbyModel.Door.DoorPart.CanCollide = false
						script.Parent.ObbyModel.Door.DoorPart.Transparency = 0.5
						local itemstoload = {}
						for i,v in pairs (game.ServerStorage.DATA_STORAGE:FindFirstChild(player.Name).PlacedItems:GetChildren()) do
							table.insert(itemstoload, v.PlaceInObby.Value, v.Name)
						end
						table.sort(itemstoload)
						for i,v in pairs(itemstoload) do
							local item = game.ServerStorage.ObbyPartStorage:FindFirstChild(v):Clone()
							script.Parent.ObbyParts.Value = script.Parent.ObbyParts.Value + 1
							item.Parent = script.Parent.ObbyPartStorage
							item:SetPrimaryPartCFrame(script.Parent.NextConnectionPart.Value.CFrame)
							item.PlaceInObby.Value = script.Parent.ObbyParts.Value
							script.Parent.NextConnectionPart.Value = item.EndPart
							script.Parent.LastPartPlaced.Value = item
						end
					end
				end
			end
		end
		db = false
	end
end)

It isn't my saving script that's why this is loading wrong, while testing I manually put in all the items in game.ServerStorage.DATA_STORAGE.lucas4114.PlacedItems , https://gyazo.com/129b3730a3c3d2f7b3dc4cf5208ee2ac

1 answer

✓ Accepted answer

Pairs is unordered
Useless.

If you need to keep stuff in order, I'd suggest you iterate with numbers as they can be considered to actually have an order. ipairs could work for you if you want an iterator format.

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