I’m trying to make a game so where when you blink another item is added, but I have it in a specific order for a storyline. The order goes from 1 to 3 (1,2,3) so why does it spawn in #1, #3, and then #2? I need help, my brain hurts too much.
game.ReplicatedStorage.OnBlink.OnServerEvent:Connect(function(plr)
local orderTable = {}
local key,value
for i,v in pairs(game.ServerStorage.Objects:GetChildren()) do
table.sort(orderTable)
table.insert(orderTable,v:GetAttribute("Order"),v.Name)
--print(v:GetAttribute("Order"))
--print(orderTable)
table.sort(orderTable)
end
key,value = next(orderTable)
print(key,value)
print(type(value))
print(value)
if game.ServerStorage.Objects:FindFirstChild(value) then
game.ServerStorage.Objects:FindFirstChild(value).Parent = workspace
end
end)
No errors, it’s purely a problem with how I wrote my code.
You’re passing an array of instances as the first argument to table.sort(), with no second argument table.sort() can only sort primitive data-sets, you need to utilise the second parameter (the sort function) which specifies how the data should be sorted, take the following for example.
local model = workspace.Model
local children = model:GetChildren()
for _, child in ipairs(children) do
print(child.Name)
end
print("\n\n")
table.sort(children, function(left, right)
return left.Name < right.Name
end)
for _, child in ipairs(children) do
print(child.Name)
end