Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do i sort this type of table?? (table.sort not work)

Asked by 3 years ago

So, I'm making a script to read the animation in module. This is the animation module.

return {
    Properties = {
        Looping = false,
        Priority = Enum.AnimationPriority.Core
    },
    Keyframes = {
        [0] = {
            ["HumanoidRootPart"] = {
                ["Torso"] = {
                },
            },
        },
        [0.5] = {
            ["HumanoidRootPart"] = {
                ["Torso"] = {
                    CFrame = CFrame.new(0, 0, 60),
                },
            },
        },
        [1.1] = {
            ["HumanoidRootPart"] = {
                ["Torso"] = {
                    CFrame = CFrame.new(0, 0, 60) * CFrame.Angles(math.rad(112.472), 0, 0),
                },
            },
        },
        [1.8] = {
            ["HumanoidRootPart"] = {
                ["Torso"] = {
                    CFrame = CFrame.new(0, -151.054, -2.569) * CFrame.Angles(math.rad(112.472), 0, 0),
                },
            },
        },
    }
}

And then i make it outputs the keyframe times of it. This is the script to output it:

local yes = require(workspace["Untitled Animation Clip"])

local t = yes.Keyframes

for i,v in pairs(t) do
    print(i)
end

Expected output:

0
0.5
1.1
1.8

Instead this is what i got

0
1.1
1.8
0.5

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

I would restructure your tables into something a little more usable something like:

{
    Properties = {
        Looping = false,
        Priority = Enum.AnimationPriority.Core
    },
    Keyframes = {
        [1] = {
            ["Time"] = 0,
            ["HumanoidRootPart"] = {
                ["Torso"] = {
                },
            },
        },
        [2] = {
            ["Time"] = 0.5,
            ["HumanoidRootPart"] = {
                ["Torso"] = {
                    CFrame = CFrame.new(0, 0, 60),
                },
            },
        },
        [3] = {
            ["Time"] = 1.1,
            ["HumanoidRootPart"] = {
                ["Torso"] = {
                    CFrame = CFrame.new(0, 0, 60) * CFrame.Angles(math.rad(112.472), 0, 0),
                },
            },
        },
        [4] = {
            ["Time"] = 1.8,
            ["HumanoidRootPart"] = {
                ["Torso"] = {
                    CFrame = CFrame.new(0, -151.054, -2.569) * CFrame.Angles(math.rad(112.472), 0, 0),
                },
            },
        },
        -- etc......
    }
}
-- this for loop is just to display what you have in your table!
for i=1, #Tab.Keyframes  do
    local Frame = Tab.Keyframes[i]
    print("==========================")
    table.foreach(Frame,function(a,b)
        print("[",i,"]",a," = ",b)
        if(typeof(b) == "table") then
            for j,n in pairs(b) do
                print("\t",j,n)
                if(typeof(n) == "table") then
                    for t,g in pairs(n) do
                        print("\t\t",t,g)
                    end
                end
            end
        end
    end)
end


So now you can keep your frames in order but you would have something that reads the Time value so it keeps things synced.

The way you've got the table structure, it's making it hard to keep the keyframes in order as you've discovered.

When I'm working on something that needs frames i always keep the frames numbered easy like 1,2,3,4 etc... then have a separate part of your table reader reading the time stamp and displaying it at that time stamp making sure it gets played at the correct time.

Hope this helps! :)

Ad

Answer this question