This tool arrangement script works but in the wrong arrangement

script:

function ChangeToolPosition(Tool, Position)
    local Tools = {}

    for i,v in pairs(Tool.Parent:GetChildren()) do
        if v:IsA("Tool") then
            table.insert(Tools, v)
        end
    end

    local OldParent = Tool.Parent

    local Number = 0

    for i,v in pairs(Tools) do
        if v:IsA("Tool") then
            Number += 1

            if Number == 1 then
                if Tools[Position + 1] == Tool then
                    Tool.Parent = script.Tools
                elseif v ~= Tool and Number ~= Position - 1 then
                    v.Parent = script.Tools
                end
            elseif v ~= Tool and Number ~= Position - 1 then
                v.Parent = script.Tools
            else
                Tools[Position].Parent = script.Tools
            end

            if Number == Position then
                Tool.Parent = script.Tools
            elseif Number == Position - 1 then
                Tools[Position].Parent = script.Tools
            end
        end
    end

    for i,v in pairs(script.Tools:GetChildren()) do
        if v:IsA("Tool") then
            v.Parent = OldParent
        end
    end
end



ChangeToolPosition(script.Parent:WaitForChild("Shoot"), script.Parent.Parent:WaitForChild("plrData"):WaitForChild("ToolArrangement"):WaitForChild("Shoot").Value)
ChangeToolPosition(script.Parent:WaitForChild("Pass"), script.Parent.Parent:WaitForChild("plrData"):WaitForChild("ToolArrangement"):WaitForChild("Pass").Value)
ChangeToolPosition(script.Parent:WaitForChild("Long"), script.Parent.Parent:WaitForChild("plrData"):WaitForChild("ToolArrangement"):WaitForChild("Long").Value)
ChangeToolPosition(script.Parent:WaitForChild("Tackle"), script.Parent.Parent:WaitForChild("plrData"):WaitForChild("ToolArrangement"):WaitForChild("Tackle").Value)
ChangeToolPosition(script.Parent:WaitForChild("Dribble"), script.Parent.Parent:WaitForChild("plrData"):WaitForChild("ToolArrangement"):WaitForChild("Dribble").Value)

This is meant for arranging tools according to a datastore value within the player, it does work but in the wrong order.

Shoot = 1
Pass = 2

it would sort it in the wrong order

Pass = 1
Shoot = 2

Doing Wait times wouldn’t work as it would break the tools. what can i do?

1 Like

You gotta make sure ur keeping track of how your array is being sorted. If u got everything as table.insert(Tools, v) it’ll chuck it right at the end of the array. Also bear in mind that Lua arrays start at 1, not 0, so if ur shooting so to say for your Shoot to be in position 1, it should be Tools[1], not Tools[0].

Try using the sort() function my dude. It can sort ur tools based off any condition u set. Here’s an example (obviously you should adapt this to your use case):

table.sort(Tools, function(a, b)
   return a[Position] < b[Position]
end)

This’ll sort your tools in ascending order based off their ‘Position’ properties. If u want it in descending order, change “<” to “>”.

1 Like

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