Objects in the explorer change order when running the game

Hello, “sorry I really dont know if this is the right category for this topic”

so I’ve made a little system that needs parts inside a folder in the correct order to work correctly
as you can see when the game isn’t running the parts are in the correct order:
Capture d’écran 2022-08-29 004624

but when I run the game for some reason all the parts change order and well it breaks my whole system:

so could anyone tell me if there is a way of preventing this from happening?

Why is your system relying on the order of a folder in that sense, if it’s a number-based system just use a for loop to iterate through -

like

for i = 1, #workspace.Nodes:GetChildren() do
    local pickedNode = workspace.Nodes:FindFirstChild(i.."Node")
    -- code here that you want to be done in order with the pickedNode
end

well its not number based it needs to get the actual parts in the right order I added the numbers just to see if its on right order or not

its like one part moving with “TweenService” and it uses all the nodes as a kind of road to follow

Can you give us a code example?

local TweenService = game:GetService("TweenService")
local Part = script.Parent


Position = Part.Nodes:GetChildren()
Get = Part.Block.B
local Time = 15


while true do
	   for i,v in pairs (Position)do 
		local Speed = (Get.CFrame.p - v.CFrame.p).Magnitude / Time
		local Info = TweenInfo.new(Speed,Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
		local Tween = TweenService:Create(Get,Info, {Rotation = v.Rotation,CFrame = v.CFrame})
	    Tween:Play()
	    Tween.Completed:Wait()
	   end
end

Its a simple server script that tweens the moving part in a loop

“I dont think that this script has anything to do with the order of the parts in the explorer changing”

As far as I know this is due to the way the game loads them in, if they were added in at intervals, I believe they stay ordered, but I’m not 100%.

Just do this -

local TweenService = game:GetService("TweenService")
local Part = script.Parent


Position = Part.Nodes:GetChildren()
Get = Part.Block.B
local Time = 15


while true do
	for i = 1, #Position do
		local v = Part.Nodes:FindFirstChild(i.."Node")
		local Speed = (Get.CFrame.p - v.CFrame.p).Magnitude / Time
		local Info = TweenInfo.new(Speed,Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
		local Tween = TweenService:Create(Get,Info, {Rotation = v.Rotation,CFrame = v.CFrame})
		Tween:Play()
		Tween.Completed:Wait()
	end
end
1 Like

it didn’t change anything, I’m starting to think that I should make it so the for loop takes the parts using the numbers in their names could I do that?

That’s literally what I just sent you…

yes, but wouldn’t a string.gsub be needed to get only the numbers from the name?

oh, I’ve just tried to use your script again and it worked this time! I think that I am the one who did something wrong here :sweat_smile: , Thanks!

GetChildren returns an array of children in an undefined order.

local Nodes = NodeFolder:GetChildren() --Get array of nodes.
table.sort(Nodes, function(L, R) --Sort array of nodes.
	local LNumber = string.match(L.Name, "^(%d+)") --Get node's index.
	local RNumber = string.match(R.Name, "^(%d+)") --Get node's index.
	return tonumber(LNumber) < tonumber(RNumber) --Less than comparison.
end)

--The nodes can now be iterated over in order wherever necessary.
for _, Node in ipairs(Nodes) do
	print(Node.Name)
end

--1Node
--2Node
--3Node
--...
1 Like