Problem with things not going in alphabetically order. (Ipairs not working)

I’m trying to make a doors similar game but these won’t go in order. it prints 2 the 1 when its supposed to go 1 then 2.

for _, Room in ipairs(game.Workspace.Rooms:GetChildren()) do
	local Table = {}
	for i, Points in ipairs(Room:FindFirstChild("RushPoints"):GetChildren()) do
		table.insert(Table,Points)
	end
	for i, Point in ipairs(Table) do
		TweenService:Create(Torso,TweenInfo.new(.5),{CFrame = Point.CFrame}):Play()
		print(Point.Name)
		wait(.5)
	end
end

Not enough information here … I’ll take a guess. See if a sort works.

for _, Room in ipairs(game.Workspace.Rooms:GetChildren()) do
    local Table = {}
    for _, Points in ipairs(Room:FindFirstChild("RushPoints"):GetChildren()) do
        table.insert(Table, Points)
    end
    table.sort(Table, function(a, b) return a.Name < b.Name end)
    for _, Point in ipairs(Table) do
        TweenService:Create(Torso, TweenInfo.new(.5), { CFrame = Point.CFrame }):Play()
        print(Point.Name)
        wait(.5)
    end
end

May not be ipairs also. Just a guess. Think the sort is all you really need.

You should use

pairs()

ipairs iterates through arrays, while pairs iterates through both arrays and dictionaries.

YESSS IT WORKS! this took forever thanks!

1 Like
local RoomsFolder = workspace.Rooms
local Rooms = RoomsFolder:GetChildren()
table.sort(Rooms, function(Left, Right)
    return tonumber(Left.Name) < tonumber(Right.Name)
end)

for _, Room in ipairs(Rooms) do
    local PointsFolder = Room.RushPoints
    local Points = PointsFolder:GetChildren()
    table.sort(Points, function(Left, Right)
        return tonumber(Left.Name) < tonumber(Right.Name)
    end)

    for _, Point in ipairs(Points) do
		local Tween = TweenService:Create(Torso, TweenInfo.new(0.5), {CFrame = Point.CFrame})
		Tween:Play()
		Tween.Completed:Wait()
	end
end

This will stop working if the number of points exceeds 9.
print("10" < "9") --true
That’s why you need to use tonumber.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.