I need help on a script, whenever I sort it doesnt sort correctly
hers my sorting part
script.Parent.Container.ChildAdded:Connect(function(child)
if child:IsA("TextButton") and child.Name ~= "Template" then
table.insert(powers,game.Players.LocalPlayer.Pets:FindFirstChild(child.Name).Value .. ":"..child.Name)
table.sort(powers, function(a, b)
return a > b
end)
print(powers[1], powers[2], powers[3], powers[4], powers[5])
end
end)
and my output is
84:Double Small Void Star 5:Cosmic Star 5:Cosmic Star 42:Small Void Star 42:Small Void Star
If you absolutely need the name of the child being added and the corresponding number value, then you could do something like this.
script.Parent.Container.ChildAdded:Connect(function(child)
if child:IsA("TextButton") and child.Name ~= "Template" then
table.insert (powers, {game.Players.LocalPlayer.Pets:FindFirstChild(child.Name).Value, child.Name})
table.sort(powers, function(a, b)
return a[1] > b[1]
end)
print(powers[1], powers[2], powers[3], powers[4], powers[5])
end
end)
If you only need the number, then this would work just as well
script.Parent.Container.ChildAdded:Connect(function(child)
if child:IsA("TextButton") and child.Name ~= "Template" then
table.insert (powers, game.Players.LocalPlayer.Pets:FindFirstChild(child.Name).Value)
table.sort(powers, function(a, b)
return a > b
end)
print(powers[1], powers[2], powers[3], powers[4], powers[5])
end
end)
-- when a child is added to Container call this function
script.Parent.Container.ChildAdded:Connect(function(child)
-- if the child is not a textbutton then exit the function
if child:IsA("TextButton") == false then return end
-- if the child name is template then exit the function
if child.Name == "Template" then return end
-- get a reference of the pet in the players pets folder
local newPet = game.Players.LocalPlayer.Pets[child.Name]
-- set the position to the end of the table
local position = #powers + 1
-- lets loop all powers to see where this new power must go
for i, pet in powers do
-- if the pet in the powers table has a value less then the new pet then skip to the next pet
if pet.Value < newPet.Value then continue end
-- set position to i
position = i
-- break out of this loop we no longer need to loop anymore
break
end
-- add the new pet to the correct position so we don't need to sort
table.insert(powers, position, newPet)
end