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:
but when I run the game for some reason all the parts change order and well it breaks my whole system:
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
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
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
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?
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
--...