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

Need Help with This Running Animation?

Asked by 10 years ago

f you ever notice the animation in Speed Run 4 I was wondering where do you put the animation script? Like if in Workspace,StarterPack,ETC

Here is the script.

function waitForChild(instance, name, rec)
    if not instance:FindFirstChild(name, rec == true) then
        repeat wait() until instance:FindFirstChild(name, rec == true)
    end
    return instance:FindFirstChild(name, rec == true)
end

function waitForProperty(instance, name)
    if not instance[name] then
        repeat wait() until instance[name]
    end
    return instance[name]
end

local rs = game:service("RunService").Stepped
local anim = waitForChild(script, "KeyframeSequence")
local play = waitForChild(script, "Play")
local stop = waitForChild(script, "Stop")
local keyr = waitForChild(script, "KeyframeReached")
local loop = waitForChild(script, "EnableLoop")
local model = waitForProperty(script, "Parent")
local fadeOut = false
local steps = {}
local start = tick()
local fadeTime = 0.1
local speed = 1
local oldstep
local mode

local offset = {
    ["Head"] = {
        c0 = CFrame.new(0, 1, 0) * CFrame.fromAxisAngle(Vector3.new(-1, 0, 0), math.pi/2),
        c1 = CFrame.new(0, -0.5, 0) * CFrame.fromAxisAngle(Vector3.new(1, 0, 0), -math.pi/2)},
    ["Torso"] = {
        c0 = CFrame.new(0, 0, 0) * CFrame.fromAxisAngle(Vector3.new(-1, 0, 0), math.pi/2),
        c1 = CFrame.new(0, 0, 0) * CFrame.fromAxisAngle(Vector3.new(1, 0, 0), -math.pi/2)},
    ["Left Leg"] = {
        c0 = CFrame.new(-0.5, -1, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2),
        c1 = CFrame.new(0, 1, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2)},
    ["Right Leg"] = {
        c0 = CFrame.new(0.5, -1, 0) * CFrame.fromAxisAngle(Vector3.new(0, -1, 0), -math.pi/2),
        c1 = CFrame.new(0, 1, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), math.pi/2)},
    ["Left Arm"] = {
        c0 = CFrame.new(-1.0, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2),
        c1 = CFrame.new(0.5, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2)},
    ["Right Arm"] = {
        c0 = CFrame.new(1.0, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, -1, 0), -math.pi/2),
        c1 = CFrame.new(-0.5, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), math.pi/2)}
}

function sortSteps(obj)
    local t, ret = 0, {}
    for k, v in next, obj:GetChildren() do
        ret[#ret + 1] = {v, t}
        t = t + v.Time
    end
    table.sort(ret, function(a, b) return a[2] < b[2] end)
    return ret
end

function jointByPart1(par, part1)
    for k, v in next, par:GetChildren() do
        if v:IsA("JointInstance") and v.Part1 == part1 then
            return v
        end
        local find = jointByPart1(v, part1)
        if find then
            return find
        end
    end
    return
end

function allposes(par)
    local t = {}
    for k, v in next, par:GetChildren() do
        if v:IsA("Pose") then
            t[tostring(v)] = tostring(v)
        end
        local other = allposes(v)
        for k, v in next, other do
            t[tostring(v)] = tostring(v)
        end
    end
    return t
end

function poses(par, assoc)
    local t = {}
    for k, v in next, par:GetChildren() do
        if v:IsA("Pose") then
            t[(assoc and tostring(v) or #t + 1)] = v
        end
        local other = poses(v)
        for k, v in next, other do
            t[(assoc and tostring(v) or #t + 1)] = v
        end
    end
    return t
end

function lerp(obj, from, to, p)
    if offset[tostring(obj)] then
        if pcall(function() return game.IsA(from, "Pose") end) then
            from = from.CFrame
        end
        local joint = jointByPart1(model, obj)
        local x1, y1, z1 = from:toEulerAnglesXYZ()
        local x2, y2, z2 = to:toEulerAnglesXYZ()
        joint.C0 = offset[tostring(obj)].c0 * CFrame.new(from.p:Lerp(to.p, p)) * CFrame.Angles(x1 + (x2 - x1) * p, y1 + (y2 - y1) * p, z1 + (z2 - z1) * p)
    end
end

rs:connect(function(timer, stepped)
    if mode or fadeOut then
        local mystep = 0
        local perc = 0
        local elaps = (tick() - start) * speed
        for k, v in next, steps do
            if elaps > v[2] and (not steps[k + 1] or (elaps <= steps[k + 1][2])) then
                mystep = k
                if not fadeOut then
                    perc = (elaps - v[2]) / (mystep == 1 and speed * fadeTime or (mystep == #steps and (not mode.Loop and speed * fadeTime) or v[1].Time))
                else
                    perc = (elaps - v[2]) / fadeTime
                end
            end
        end
        if mystep ~= oldstep then
            oldstep = mystep
            keyr:Fire(tostring(steps[mystep][1]))
        end
        if mystep == #steps or fadeOut then
            if not fadeOut and mode and mode.Loop then
                start = tick()
            elseif perc <= 1 then
                for k, v in next, allposes(mode) do
                    if model:FindFirstChild(tostring(v)) and model:FindFirstChild(tostring(v)):IsA("BasePart") and offset[tostring(v)] then
                        lerp(model:FindFirstChild(tostring(v)),
                            (steps[mystep - 1] and poses(steps[mystep - 1][1], true)[tostring(v)] or CFrame.new()),
                            CFrame.new(),
                            perc)
                    end
                end
            else
                for k, v in next, allposes(mode) do
                    if model:FindFirstChild(tostring(v)) and model:FindFirstChild(tostring(v)):IsA("BasePart") and offset[tostring(v)] then
                        lerp(model:FindFirstChild(tostring(v)),
                            (steps[mystep - 1] and poses(steps[mystep - 1][1], true)[tostring(v)] or CFrame.new()),
                            CFrame.new(),
                            1)
                    end
                end
                mode = nil
                oldstep = nil
                fadeOut = false
            end
        elseif mode then
            for k, v in next, poses(steps[mystep][1]) do
                if model:FindFirstChild(tostring(v)) and model:FindFirstChild(tostring(v)):IsA("BasePart") and offset[tostring(v)] then
                    lerp(model:FindFirstChild(tostring(v)),
                        (steps[mystep - 1] and poses(steps[mystep - 1][1], true)[tostring(v)] or CFrame.new()),
                        v.CFrame,
                        perc)
                end
            end
        end
    end
end)

play.Event:connect(function(_fadeTime, _speed)
    if not mode then
        start = tick()
        steps = sortSteps(anim)
        oldstep = nil
        fadeTime = type(_fadeTime) == "number" and _fadeTime or 0.1
        speed = type(_speed) == "number" and _speed or 1
        mode = anim
    end
end)

stop.Event:connect(function()
    oldstep = nil
    fadeOut = true
    repeat
        rs:wait()
    until not fadeOut
    mode = nil
    speed = 1
    fadeTime = 0.1
    -- do stuff?
end)

loop.Event:connect(function(on) -- to skip annoying stuff.
    anim.Loop = not not on
end)

Answer this question