Advertisement
iaminonlinetoorare

Untitled

Mar 10th, 2025
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.62 KB | None | 0 0
  1. --[[ i skidded it from someone idk ]]--
  2. local plr = game:GetService("Players").LocalPlayer
  3.  
  4. function RunCustomAnimation(Char)
  5.     if Char:WaitForChild("Animate") ~= nil then
  6.         Char.Animate.Disabled = true
  7.     end
  8.    
  9.     Char:WaitForChild("Humanoid")
  10.  
  11.     for i,v in next, Char.Humanoid:GetPlayingAnimationTracks() do
  12.         v:Stop()
  13.     end
  14.  
  15.     --fake script
  16.     local script = Char.Animate
  17.  
  18.     local Character = Char
  19.     local Humanoid = Character:WaitForChild("Humanoid")
  20.     local pose = "Standing"
  21.  
  22.     local UserGameSettings = UserSettings():GetService("UserGameSettings")
  23.  
  24.     local userNoUpdateOnLoopSuccess, userNoUpdateOnLoopValue = pcall(function() return UserSettings():IsUserFeatureEnabled("UserNoUpdateOnLoop") end)
  25.     local userNoUpdateOnLoop = userNoUpdateOnLoopSuccess and userNoUpdateOnLoopValue
  26.  
  27.     local AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
  28.     local HumanoidHipHeight = 2
  29.  
  30.     local humanoidSpeed = 0 -- speed most recently sent to us from onRunning()
  31.     local cachedRunningSpeed = 0 -- The most recent speed used to compute blends.  Tiny variations from cachedRunningSpeed will not cause animation updates.
  32.     local cachedLocalDirection = {x=0.0, y=0.0} -- unit 2D object space direction of motion
  33.     local smallButNotZero = 0.0001 -- We want weights to be small but not so small the animation stops
  34.     local runBlendtime = 0.2
  35.     local lastLookVector = Vector3.new(0.0, 0.0, 0.0) -- used to track whether rootPart orientation is changing.
  36.     local lastBlendTime = 0 -- The last time we blended velocities
  37.     local WALK_SPEED = 6.4
  38.     local RUN_SPEED = 12.8
  39.  
  40.     local EMOTE_TRANSITION_TIME = 0.1
  41.  
  42.     local currentAnim = ""
  43.     local currentAnimInstance = nil
  44.     local currentAnimTrack = nil
  45.     local currentAnimKeyframeHandler = nil
  46.     local currentAnimSpeed = 1.0
  47.  
  48.     local PreloadedAnims = {}
  49.  
  50.     local animTable = {}
  51.     local animNames = {
  52.         idle =  {
  53.             { id = "http://www.roblox.com/asset/?id=12521158637", weight = 9 },
  54.             { id = "http://www.roblox.com/asset/?id=12521162526", weight = 1 },
  55.         },
  56.         walk =  {
  57.             { id = "http://www.roblox.com/asset/?id=12518152696", weight = 10 }
  58.         },
  59.         run =   {
  60.             { id = "http://www.roblox.com/asset/?id=12518152696", weight = 10 }
  61.         },
  62.         jump =  {
  63.             { id = "http://www.roblox.com/asset/?id=12520880485", weight = 10 }
  64.         },
  65.         fall =  {
  66.             { id = "http://www.roblox.com/asset/?id=12520972571", weight = 10 }
  67.         },
  68.         climb = {
  69.             { id = "http://www.roblox.com/asset/?id=12520982150", weight = 10 }
  70.         },
  71.         sit =   {
  72.             { id = "http://www.roblox.com/asset/?id=12520993168", weight = 10 }
  73.         },
  74.         toolnone = {
  75.             { id = "http://www.roblox.com/asset/?id=12520996634", weight = 10 }
  76.         },
  77.         toolslash = {
  78.             { id = "http://www.roblox.com/asset/?id=12520999032", weight = 10 }
  79.         },
  80.         toollunge = {
  81.             { id = "http://www.roblox.com/asset/?id=12521002003", weight = 10 }
  82.         },
  83.         wave = {
  84.             { id = "http://www.roblox.com/asset/?id=12521004586", weight = 10 }
  85.         },
  86.         point = {
  87.             { id = "http://www.roblox.com/asset/?id=12521007694", weight = 10 }
  88.         },
  89.         dance = {
  90.             { id = "http://www.roblox.com/asset/?id=12521009666", weight = 10 },
  91.             { id = "http://www.roblox.com/asset/?id=12521151637", weight = 10 },
  92.             { id = "http://www.roblox.com/asset/?id=12521015053", weight = 10 }
  93.         },
  94.         dance2 = {
  95.             { id = "http://www.roblox.com/asset/?id=12521169800", weight = 10 },
  96.             { id = "http://www.roblox.com/asset/?id=12521173533", weight = 10 },
  97.             { id = "http://www.roblox.com/asset/?id=12521027874", weight = 10 }
  98.         },
  99.         dance3 = {
  100.             { id = "http://www.roblox.com/asset/?id=12521178362", weight = 10 },
  101.             { id = "http://www.roblox.com/asset/?id=12521181508", weight = 10 },
  102.             { id = "http://www.roblox.com/asset/?id=12521184133", weight = 10 }
  103.         },
  104.         laugh = {
  105.             { id = "http://www.roblox.com/asset/?id=12521018724", weight = 10 }
  106.         },
  107.         cheer = {
  108.             { id = "http://www.roblox.com/asset/?id=12521021991", weight = 10 }
  109.         },
  110.     }
  111.  
  112.  
  113.     local strafingLocomotionMap = {}
  114.     local fallbackLocomotionMap = {}
  115.     local locomotionMap = strafingLocomotionMap
  116.     -- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
  117.     local emoteNames = { wave = false, point = false, dance = true, dance2 = true, dance3 = true, laugh = false, cheer = false}
  118.  
  119.     math.randomseed(tick())
  120.  
  121.     function findExistingAnimationInSet(set, anim)
  122.         if set == nil or anim == nil then
  123.             return 0
  124.         end
  125.  
  126.         for idx = 1, set.count, 1 do
  127.             if set[idx].anim.AnimationId == anim.AnimationId then
  128.                 return idx
  129.             end
  130.         end
  131.  
  132.         return 0
  133.     end
  134.  
  135.     function configureAnimationSet(name, fileList)
  136.         if (animTable[name] ~= nil) then
  137.             for _, connection in pairs(animTable[name].connections) do
  138.                 connection:disconnect()
  139.             end
  140.         end
  141.         animTable[name] = {}
  142.         animTable[name].count = 0
  143.         animTable[name].totalWeight = 0
  144.         animTable[name].connections = {}
  145.  
  146.         -- uncomment this section to allow players to load with their
  147.         -- own (non-classic) animations
  148.         --[[
  149.         local config = script:FindFirstChild(name)
  150.         if (config ~= nil) then
  151.             table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  152.             table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  153.  
  154.             local idx = 0
  155.  
  156.             for _, childPart in pairs(config:GetChildren()) do
  157.                 if (childPart:IsA("Animation")) then
  158.                     local newWeight = 1
  159.                     local weightObject = childPart:FindFirstChild("Weight")
  160.                     if (weightObject ~= nil) then
  161.                         newWeight = weightObject.Value
  162.                     end
  163.                     animTable[name].count = animTable[name].count + 1
  164.                     idx = animTable[name].count
  165.                     animTable[name][idx] = {}
  166.                     animTable[name][idx].anim = childPart
  167.                     animTable[name][idx].weight = newWeight
  168.                     animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  169.                     table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  170.                     table.insert(animTable[name].connections, childPart.ChildAdded:connect(function(property) configureAnimationSet(name, fileList) end))
  171.                     table.insert(animTable[name].connections, childPart.ChildRemoved:connect(function(property) configureAnimationSet(name, fileList) end))
  172.                     local lv = childPart:GetAttribute("LinearVelocity")
  173.                     if lv then
  174.                         strafingLocomotionMap[name] = {lv=lv, speed = lv.Magnitude}
  175.                     end
  176.                     if name == "run" or name == "walk" then
  177.  
  178.                         if lv then
  179.                             fallbackLocomotionMap[name] = strafingLocomotionMap[name]
  180.                         else
  181.                             local speed = name == "run" and RUN_SPEED or WALK_SPEED
  182.                             fallbackLocomotionMap[name] = {lv=Vector2.new(0.0, speed), speed = speed}
  183.                             locomotionMap = fallbackLocomotionMap
  184.                             -- If you don't have a linear velocity with your run or walk, you can't blend/strafe
  185.                             --warn("Strafe blending disabled. No linear velocity information for "..'"'.."walk"..'"'.." and "..'"'.."run"..'"'..".")
  186.                         end
  187.  
  188.                     end
  189.                 end
  190.             end
  191.         end
  192.         ]]
  193.  
  194.         -- if you uncomment the above section, comment out this "if"-block
  195.         if name == "run" or name == "walk" then
  196.             local speed = name == "run" and RUN_SPEED or WALK_SPEED
  197.             fallbackLocomotionMap[name] = {lv=Vector2.new(0.0, speed), speed = speed}
  198.             locomotionMap = fallbackLocomotionMap
  199.             -- If you don't have a linear velocity with your run or walk, you can't blend/strafe
  200.             --warn("Strafe blending disabled. No linear velocity information for "..'"'.."walk"..'"'.." and "..'"'.."run"..'"'..".")
  201.         end
  202.  
  203.  
  204.         -- fallback to defaults
  205.         if (animTable[name].count <= 0) then
  206.             for idx, anim in pairs(fileList) do
  207.                 animTable[name][idx] = {}
  208.                 animTable[name][idx].anim = Instance.new("Animation")
  209.                 animTable[name][idx].anim.Name = name
  210.                 animTable[name][idx].anim.AnimationId = anim.id
  211.                 animTable[name][idx].weight = anim.weight
  212.                 animTable[name].count = animTable[name].count + 1
  213.                 animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  214.             end
  215.         end
  216.  
  217.         -- preload anims
  218.         for i, animType in pairs(animTable) do
  219.             for idx = 1, animType.count, 1 do
  220.                 if PreloadedAnims[animType[idx].anim.AnimationId] == nil then
  221.                     Humanoid:LoadAnimation(animType[idx].anim)
  222.                     PreloadedAnims[animType[idx].anim.AnimationId] = true
  223.                 end
  224.             end
  225.         end
  226.     end
  227.  
  228.     -- Setup animation objects
  229.     function scriptChildModified(child)
  230.         local fileList = animNames[child.Name]
  231.         if (fileList ~= nil) then
  232.             configureAnimationSet(child.Name, fileList)
  233.         else
  234.             if child:isA("StringValue") then
  235.                 animNames[child.Name] = {}
  236.                 configureAnimationSet(child.Name, animNames[child.Name])
  237.             end
  238.         end
  239.     end
  240.  
  241.     script.ChildAdded:connect(scriptChildModified)
  242.     script.ChildRemoved:connect(scriptChildModified)
  243.  
  244.     -- Clear any existing animation tracks
  245.     -- Fixes issue with characters that are moved in and out of the Workspace accumulating tracks
  246.     local animator = if Humanoid then Humanoid:FindFirstChildOfClass("Animator") else nil
  247.     if animator then
  248.         local animTracks = animator:GetPlayingAnimationTracks()
  249.         for i,track in ipairs(animTracks) do
  250.             track:Stop(0)
  251.             track:Destroy()
  252.         end
  253.     end
  254.  
  255.     for name, fileList in pairs(animNames) do
  256.         configureAnimationSet(name, fileList)
  257.     end
  258.     for _,child in script:GetChildren() do
  259.         if child:isA("StringValue") and not animNames[child.name] then
  260.             animNames[child.Name] = {}
  261.             configureAnimationSet(child.Name, animNames[child.Name])
  262.         end
  263.     end
  264.  
  265.     -- ANIMATION
  266.  
  267.     -- declarations
  268.     local toolAnim = "None"
  269.     local toolAnimTime = 0
  270.  
  271.     local jumpAnimTime = 0
  272.     local jumpAnimDuration = 0.31
  273.  
  274.     local toolTransitionTime = 0.1
  275.     local fallTransitionTime = 0.2
  276.  
  277.     local currentlyPlayingEmote = false
  278.  
  279.     -- functions
  280.  
  281.     function stopAllAnimations()
  282.         local oldAnim = currentAnim
  283.  
  284.         -- return to idle if finishing an emote
  285.         if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  286.             oldAnim = "idle"
  287.         end
  288.  
  289.         if currentlyPlayingEmote then
  290.             oldAnim = "idle"
  291.             currentlyPlayingEmote = false
  292.         end
  293.  
  294.         currentAnim = ""
  295.         currentAnimInstance = nil
  296.         if (currentAnimKeyframeHandler ~= nil) then
  297.             currentAnimKeyframeHandler:disconnect()
  298.         end
  299.  
  300.         if (currentAnimTrack ~= nil) then
  301.             currentAnimTrack:Stop()
  302.             currentAnimTrack:Destroy()
  303.             currentAnimTrack = nil
  304.         end
  305.  
  306.         for _,v in pairs(locomotionMap) do
  307.             if v.track then
  308.                 v.track:Stop()
  309.                 v.track:Destroy()
  310.                 v.track = nil
  311.             end
  312.         end
  313.  
  314.         return oldAnim
  315.     end
  316.  
  317.     function getHeightScale()
  318.         if Humanoid then
  319.             if not Humanoid.AutomaticScalingEnabled then
  320.                 return 1
  321.             end
  322.  
  323.             local scale = Humanoid.HipHeight / HumanoidHipHeight
  324.             if AnimationSpeedDampeningObject == nil then
  325.                 AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
  326.             end
  327.             if AnimationSpeedDampeningObject ~= nil then
  328.                 scale = 1 + (Humanoid.HipHeight - HumanoidHipHeight) * AnimationSpeedDampeningObject.Value / HumanoidHipHeight
  329.             end
  330.             return scale
  331.         end
  332.         return 1
  333.     end
  334.  
  335.  
  336.     local function signedAngle(a, b)
  337.         return -math.atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y)
  338.     end
  339.  
  340.     local angleWeight = 2.0
  341.     local function get2DWeight(px, p1, p2, sx, s1, s2)
  342.         local avgLength = 0.5 * (s1 + s2)
  343.  
  344.         local p_1 = {x = (sx - s1)/avgLength, y = (angleWeight * signedAngle(p1, px))}
  345.         local p12 = {x = (s2 - s1)/avgLength, y = (angleWeight * signedAngle(p1, p2))} 
  346.         local denom = smallButNotZero + (p12.x*p12.x + p12.y*p12.y)
  347.         local numer = p_1.x * p12.x + p_1.y * p12.y
  348.         local r = math.clamp(1.0 - numer/denom, 0.0, 1.0)
  349.         return r
  350.     end
  351.  
  352.     local function blend2D(targetVelo, targetSpeed)
  353.         local h = {}
  354.         local sum = 0.0
  355.         for n,v1 in pairs(locomotionMap) do
  356.             if targetVelo.x * v1.lv.x < 0.0 or targetVelo.y * v1.lv.y < 0 then
  357.                 -- Require same quadrant as target
  358.                 h[n] = 0.0
  359.                 continue
  360.             end
  361.             h[n] = math.huge
  362.             for j,v2 in pairs(locomotionMap) do
  363.                 if targetVelo.x * v2.lv.x < 0.0 or targetVelo.y * v2.lv.y < 0 then
  364.                     -- Require same quadrant as target
  365.                     continue
  366.                 end
  367.                 h[n] = math.min(h[n], get2DWeight(targetVelo, v1.lv, v2.lv, targetSpeed, v1.speed, v2.speed))
  368.             end
  369.             sum += h[n]
  370.         end
  371.  
  372.         --truncates below 10% contribution
  373.         local sum2 = 0.0
  374.         local weightedVeloX = 0
  375.         local weightedVeloY = 0
  376.         for n,v in pairs(locomotionMap) do
  377.  
  378.             if (h[n] / sum > 0.1) then
  379.                 sum2 += h[n]
  380.                 weightedVeloX += h[n] * v.lv.x
  381.                 weightedVeloY += h[n] * v.lv.y
  382.             else
  383.                 h[n] = 0.0
  384.             end
  385.         end
  386.         local animSpeed
  387.         local weightedSpeedSquared = weightedVeloX * weightedVeloX + weightedVeloY * weightedVeloY
  388.         if weightedSpeedSquared > smallButNotZero then
  389.             animSpeed = math.sqrt(targetSpeed * targetSpeed / weightedSpeedSquared)
  390.         else
  391.             animSpeed = 0
  392.         end
  393.  
  394.         animSpeed = animSpeed / getHeightScale()
  395.         local groupTimePosition = 0
  396.         for n,v in pairs(locomotionMap) do
  397.             if v.track.IsPlaying then
  398.                 groupTimePosition = v.track.TimePosition
  399.                 break
  400.             end
  401.         end
  402.         for n,v in pairs(locomotionMap) do
  403.             -- if not loco
  404.             if h[n] > 0.0 then
  405.                 if not v.track.IsPlaying then
  406.                     v.track:Play(runBlendtime)
  407.                     v.track.TimePosition = groupTimePosition
  408.                 end
  409.  
  410.                 local weight = math.max(smallButNotZero, h[n] / sum2)
  411.                 v.track:AdjustWeight(weight, runBlendtime)
  412.                 v.track:AdjustSpeed(animSpeed)
  413.             else
  414.                 v.track:Stop(runBlendtime)
  415.             end
  416.         end
  417.  
  418.     end
  419.  
  420.     local function getWalkDirection()
  421.         local walkToPoint = Humanoid.WalkToPoint
  422.         local walkToPart = Humanoid.WalkToPart
  423.         if Humanoid.MoveDirection ~= Vector3.zero then
  424.             return Humanoid.MoveDirection
  425.         elseif walkToPart or walkToPoint ~= Vector3.zero then
  426.             local destination
  427.             if walkToPart then
  428.                 destination = walkToPart.CFrame:PointToWorldSpace(walkToPoint)
  429.             else
  430.                 destination = walkToPoint
  431.             end
  432.             local moveVector = Vector3.zero
  433.             if Humanoid.RootPart then
  434.                 moveVector = destination - Humanoid.RootPart.CFrame.Position
  435.                 moveVector = Vector3.new(moveVector.x, 0.0, moveVector.z)
  436.                 local mag = moveVector.Magnitude
  437.                 if mag > 0.01 then
  438.                     moveVector /= mag
  439.                 end
  440.             end
  441.             return moveVector
  442.         else
  443.             return Humanoid.MoveDirection
  444.         end
  445.     end
  446.  
  447.     local function updateVelocity(currentTime)
  448.  
  449.         local tempDir
  450.  
  451.         if locomotionMap == strafingLocomotionMap then
  452.  
  453.             local moveDirection = getWalkDirection()
  454.  
  455.             if not Humanoid.RootPart then
  456.                 return
  457.             end
  458.  
  459.             local cframe = Humanoid.RootPart.CFrame
  460.             if math.abs(cframe.UpVector.Y) < smallButNotZero or pose ~= "Running" or humanoidSpeed < 0.001 then
  461.                 -- We are horizontal!  Do something  (turn off locomotion)
  462.                 for n,v in pairs(locomotionMap) do
  463.                     if v.track then
  464.                         v.track:AdjustWeight(smallButNotZero, runBlendtime)
  465.                     end
  466.                 end
  467.                 return
  468.             end
  469.             local lookat = cframe.LookVector
  470.             local direction = Vector3.new(lookat.X, 0.0, lookat.Z)
  471.             direction = direction / direction.Magnitude --sensible upVector means this is non-zero.
  472.             local ly = moveDirection:Dot(direction)
  473.             if ly <= 0.0 and ly > -0.05 then
  474.                 ly = smallButNotZero -- break quadrant ties in favor of forward-friendly strafes
  475.             end
  476.             local lx = direction.X*moveDirection.Z - direction.Z*moveDirection.X
  477.             local tempDir = Vector2.new(lx, ly) -- root space moveDirection
  478.             local delta = Vector2.new(tempDir.x-cachedLocalDirection.x, tempDir.y-cachedLocalDirection.y)
  479.             -- Time check serves the purpose of the old keyframeReached sync check, as it syncs anim timePosition
  480.             if delta:Dot(delta) > 0.001 or math.abs(humanoidSpeed - cachedRunningSpeed) > 0.01 or currentTime - lastBlendTime > 1 then
  481.                 cachedLocalDirection = tempDir
  482.                 cachedRunningSpeed = humanoidSpeed
  483.                 lastBlendTime = currentTime
  484.                 blend2D(cachedLocalDirection, cachedRunningSpeed)
  485.             end
  486.         else
  487.             if math.abs(humanoidSpeed - cachedRunningSpeed) > 0.01 or currentTime - lastBlendTime > 1 then
  488.                 cachedRunningSpeed = humanoidSpeed
  489.                 lastBlendTime = currentTime
  490.                 blend2D(Vector2.yAxis, cachedRunningSpeed)
  491.             end
  492.         end
  493.     end
  494.  
  495.     function setAnimationSpeed(speed)
  496.         if currentAnim ~= "walk" then
  497.             if speed ~= currentAnimSpeed then
  498.                 currentAnimSpeed = speed
  499.                 currentAnimTrack:AdjustSpeed(currentAnimSpeed)
  500.             end
  501.         end
  502.     end
  503.  
  504.     function keyFrameReachedFunc(frameName)
  505.         if (frameName == "End") then
  506.             local repeatAnim = currentAnim
  507.             -- return to idle if finishing an emote
  508.             if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
  509.                 repeatAnim = "idle"
  510.             end
  511.  
  512.             if currentlyPlayingEmote then
  513.                 if currentAnimTrack.Looped then
  514.                     -- Allow the emote to loop
  515.                     return
  516.                 end
  517.  
  518.                 repeatAnim = "idle"
  519.                 currentlyPlayingEmote = false
  520.             end
  521.  
  522.             local animSpeed = currentAnimSpeed
  523.             playAnimation(repeatAnim, 0.15, Humanoid)
  524.             setAnimationSpeed(animSpeed)
  525.         end
  526.     end
  527.  
  528.     function rollAnimation(animName)
  529.         local roll = math.random(1, animTable[animName].totalWeight)
  530.         local origRoll = roll
  531.         local idx = 1
  532.         while (roll > animTable[animName][idx].weight) do
  533.             roll = roll - animTable[animName][idx].weight
  534.             idx = idx + 1
  535.         end
  536.         return idx
  537.     end
  538.  
  539.     local maxVeloX, minVeloX, maxVeloY, minVeloY
  540.  
  541.     local function destroyRunAnimations()
  542.         for _,v in pairs(strafingLocomotionMap) do
  543.             if v.track then
  544.                 v.track:Stop()
  545.                 v.track:Destroy()
  546.                 v.track = nil
  547.             end
  548.         end
  549.         for _,v in pairs(fallbackLocomotionMap) do
  550.             if v.track then
  551.                 v.track:Stop()
  552.                 v.track:Destroy()
  553.                 v.track = nil
  554.             end
  555.         end
  556.         cachedRunningSpeed = 0
  557.     end
  558.  
  559.     local function resetVelocityBounds(velo)
  560.         minVeloX = 0
  561.         maxVeloX = 0
  562.         minVeloY = 0
  563.         maxVeloY = 0
  564.     end
  565.  
  566.     local function updateVelocityBounds(velo)
  567.         if velo then
  568.             if velo.x > maxVeloX then maxVeloX = velo.x end
  569.             if velo.y > maxVeloY then maxVeloY = velo.y end
  570.             if velo.x < minVeloX then minVeloX = velo.x end
  571.             if velo.y < minVeloY then minVeloY = velo.y end
  572.         end
  573.     end
  574.  
  575.     local function checkVelocityBounds(velo)
  576.         if maxVeloX == 0 or minVeloX == 0 or maxVeloY == 0 or minVeloY == 0 then
  577.             if locomotionMap == strafingLocomotionMap then
  578.                 warn("Strafe blending disabled.  Not all quadrants of motion represented.")
  579.             end
  580.             locomotionMap = fallbackLocomotionMap
  581.         else
  582.             locomotionMap = strafingLocomotionMap
  583.         end
  584.     end
  585.  
  586.     local function setupWalkAnimation(anim, animName, transitionTime, humanoid)
  587.         resetVelocityBounds()
  588.         -- check to see if we need to blend a walk/run animation
  589.         for n,v in pairs(locomotionMap) do
  590.             v.track = humanoid:LoadAnimation(animTable[n][1].anim)
  591.             v.track.Priority = Enum.AnimationPriority.Core
  592.             updateVelocityBounds(v.lv)
  593.         end
  594.         checkVelocityBounds()
  595.     end
  596.  
  597.     local function switchToAnim(anim, animName, transitionTime, humanoid)
  598.         -- switch animation    
  599.         if (anim ~= currentAnimInstance) then
  600.  
  601.             if (currentAnimTrack ~= nil) then
  602.                 currentAnimTrack:Stop(transitionTime)
  603.                 currentAnimTrack:Destroy()
  604.             end
  605.             if (currentAnimKeyframeHandler ~= nil) then
  606.                 currentAnimKeyframeHandler:disconnect()
  607.             end
  608.  
  609.  
  610.             currentAnimSpeed = 1.0
  611.  
  612.             currentAnim = animName
  613.             currentAnimInstance = anim  -- nil in the case of locomotion
  614.  
  615.             if animName == "walk" then
  616.                 setupWalkAnimation(anim, animName, transitionTime, humanoid)
  617.             else
  618.                 destroyRunAnimations()
  619.                 -- load it to the humanoid; get AnimationTrack
  620.                 currentAnimTrack = humanoid:LoadAnimation(anim)
  621.                 currentAnimTrack.Priority = Enum.AnimationPriority.Core
  622.  
  623.                 currentAnimTrack:Play(transitionTime)  
  624.  
  625.                 -- set up keyframe name triggers
  626.                 currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  627.             end
  628.         end
  629.     end
  630.  
  631.     function playAnimation(animName, transitionTime, humanoid)
  632.         local idx = rollAnimation(animName)
  633.         local anim = animTable[animName][idx].anim
  634.  
  635.         switchToAnim(anim, animName, transitionTime, humanoid)
  636.         currentlyPlayingEmote = false
  637.     end
  638.  
  639.     function playEmote(emoteAnim, transitionTime, humanoid)
  640.         switchToAnim(emoteAnim, emoteAnim.Name, transitionTime, humanoid)
  641.         currentlyPlayingEmote = true
  642.     end
  643.  
  644.     -------------------------------------------------------------------------------------------
  645.     -------------------------------------------------------------------------------------------
  646.  
  647.     local toolAnimName = ""
  648.     local toolAnimTrack = nil
  649.     local toolAnimInstance = nil
  650.     local currentToolAnimKeyframeHandler = nil
  651.  
  652.     function toolKeyFrameReachedFunc(frameName)
  653.         if (frameName == "End") then
  654.             playToolAnimation(toolAnimName, 0.0, Humanoid)
  655.         end
  656.     end
  657.  
  658.  
  659.     function playToolAnimation(animName, transitionTime, humanoid, priority)
  660.         local idx = rollAnimation(animName)
  661.         local anim = animTable[animName][idx].anim
  662.  
  663.         if (toolAnimInstance ~= anim) then
  664.  
  665.             if (toolAnimTrack ~= nil) then
  666.                 toolAnimTrack:Stop()
  667.                 toolAnimTrack:Destroy()
  668.                 transitionTime = 0
  669.             end
  670.  
  671.             -- load it to the humanoid; get AnimationTrack
  672.             toolAnimTrack = humanoid:LoadAnimation(anim)
  673.             if priority then
  674.                 toolAnimTrack.Priority = priority
  675.             end
  676.  
  677.             -- play the animation
  678.             toolAnimTrack:Play(transitionTime)
  679.             toolAnimName = animName
  680.             toolAnimInstance = anim
  681.  
  682.             currentToolAnimKeyframeHandler = toolAnimTrack.KeyframeReached:connect(toolKeyFrameReachedFunc)
  683.         end
  684.     end
  685.  
  686.     function stopToolAnimations()
  687.         local oldAnim = toolAnimName
  688.  
  689.         if (currentToolAnimKeyframeHandler ~= nil) then
  690.             currentToolAnimKeyframeHandler:disconnect()
  691.         end
  692.  
  693.         toolAnimName = ""
  694.         toolAnimInstance = nil
  695.         if (toolAnimTrack ~= nil) then
  696.             toolAnimTrack:Stop()
  697.             toolAnimTrack:Destroy()
  698.             toolAnimTrack = nil
  699.         end
  700.  
  701.         return oldAnim
  702.     end
  703.  
  704.     -------------------------------------------------------------------------------------------
  705.     -------------------------------------------------------------------------------------------
  706.     -- STATE CHANGE HANDLERS
  707.  
  708.     function onRunning(speed)
  709.         local movedDuringEmote = currentlyPlayingEmote and Humanoid.MoveDirection == Vector3.new(0, 0, 0)
  710.         local speedThreshold = movedDuringEmote and Humanoid.WalkSpeed or 0.75
  711.         humanoidSpeed = speed
  712.         if speed > speedThreshold then
  713.             playAnimation("walk", 0.2, Humanoid)
  714.             if pose ~= "Running" then
  715.                 pose = "Running"
  716.                 updateVelocity(0) -- Force velocity update in response to state change
  717.             end
  718.         else
  719.             if emoteNames[currentAnim] == nil and not currentlyPlayingEmote then
  720.                 playAnimation("idle", 0.2, Humanoid)
  721.                 pose = "Standing"
  722.             end
  723.         end
  724.  
  725.  
  726.  
  727.     end
  728.  
  729.     function onDied()
  730.         pose = "Dead"
  731.     end
  732.  
  733.     function onJumping()
  734.         playAnimation("jump", 0.1, Humanoid)
  735.         jumpAnimTime = jumpAnimDuration
  736.         pose = "Jumping"
  737.     end
  738.  
  739.     function onClimbing(speed)
  740.         local scale = 5.0
  741.         playAnimation("climb", 0.1, Humanoid)
  742.         setAnimationSpeed(speed / scale)
  743.         pose = "Climbing"
  744.     end
  745.  
  746.     function onGettingUp()
  747.         pose = "GettingUp"
  748.     end
  749.  
  750.     function onFreeFall()
  751.         if (jumpAnimTime <= 0) then
  752.             playAnimation("fall", fallTransitionTime, Humanoid)
  753.         end
  754.         pose = "FreeFall"
  755.     end
  756.  
  757.     function onFallingDown()
  758.         pose = "FallingDown"
  759.     end
  760.  
  761.     function onSeated()
  762.         pose = "Seated"
  763.     end
  764.  
  765.     function onPlatformStanding()
  766.         pose = "PlatformStanding"
  767.     end
  768.  
  769.     -------------------------------------------------------------------------------------------
  770.     -------------------------------------------------------------------------------------------
  771.  
  772.     function onSwimming(speed)
  773.         if speed > 0 then
  774.             pose = "Running"
  775.         else
  776.             pose = "Standing"
  777.         end
  778.     end
  779.  
  780.     function animateTool()
  781.         if (toolAnim == "None") then
  782.             playToolAnimation("toolnone", toolTransitionTime, Humanoid, Enum.AnimationPriority.Idle)
  783.             return
  784.         end
  785.  
  786.         if (toolAnim == "Slash") then
  787.             playToolAnimation("toolslash", 0, Humanoid, Enum.AnimationPriority.Action)
  788.             return
  789.         end
  790.  
  791.         if (toolAnim == "Lunge") then
  792.             playToolAnimation("toollunge", 0, Humanoid, Enum.AnimationPriority.Action)
  793.             return
  794.         end
  795.     end
  796.  
  797.     function getToolAnim(tool)
  798.         for _, c in ipairs(tool:GetChildren()) do
  799.             if c.Name == "toolanim" and c.className == "StringValue" then
  800.                 return c
  801.             end
  802.         end
  803.         return nil
  804.     end
  805.  
  806.     local lastTick = 0
  807.  
  808.     function stepAnimate(currentTime)
  809.         local amplitude = 1
  810.         local frequency = 1
  811.         local deltaTime = currentTime - lastTick
  812.         lastTick = currentTime
  813.  
  814.         local climbFudge = 0
  815.         local setAngles = false
  816.  
  817.         if (jumpAnimTime > 0) then
  818.             jumpAnimTime = jumpAnimTime - deltaTime
  819.         end
  820.  
  821.         if (pose == "FreeFall" and jumpAnimTime <= 0) then
  822.             playAnimation("fall", fallTransitionTime, Humanoid)
  823.         elseif (pose == "Seated") then
  824.             playAnimation("sit", 0.5, Humanoid)
  825.             return
  826.         elseif (pose == "Running") then
  827.             playAnimation("walk", 0.2, Humanoid)
  828.             updateVelocity(currentTime)
  829.         elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  830.             stopAllAnimations()
  831.             amplitude = 0.1
  832.             frequency = 1
  833.             setAngles = true
  834.         end
  835.  
  836.         -- Tool Animation handling
  837.         local tool = Character:FindFirstChildOfClass("Tool")
  838.         if tool and tool:FindFirstChild("Handle") then
  839.             local animStringValueObject = getToolAnim(tool)
  840.  
  841.             if animStringValueObject then
  842.                 toolAnim = animStringValueObject.Value
  843.                 -- message recieved, delete StringValue
  844.                 animStringValueObject.Parent = nil
  845.                 toolAnimTime = currentTime + .3
  846.             end
  847.  
  848.             if currentTime > toolAnimTime then
  849.                 toolAnimTime = 0
  850.                 toolAnim = "None"
  851.             end
  852.  
  853.             animateTool()
  854.         else
  855.             stopToolAnimations()
  856.             toolAnim = "None"
  857.             toolAnimInstance = nil
  858.             toolAnimTime = 0
  859.         end
  860.     end
  861.  
  862.  
  863.     -- connect events
  864.     Humanoid.Died:connect(onDied)
  865.     Humanoid.Running:connect(onRunning)
  866.     Humanoid.Jumping:connect(onJumping)
  867.     Humanoid.Climbing:connect(onClimbing)
  868.     Humanoid.GettingUp:connect(onGettingUp)
  869.     Humanoid.FreeFalling:connect(onFreeFall)
  870.     Humanoid.FallingDown:connect(onFallingDown)
  871.     Humanoid.Seated:connect(onSeated)
  872.     Humanoid.PlatformStanding:connect(onPlatformStanding)
  873.     Humanoid.Swimming:connect(onSwimming)
  874.  
  875.     -- setup emote chat hook
  876.     game:GetService("Players").LocalPlayer.Chatted:connect(function(msg)
  877.         local emote = ""
  878.         if (string.sub(msg, 1, 3) == "/e ") then
  879.             emote = string.sub(msg, 4)
  880.         elseif (string.sub(msg, 1, 7) == "/emote ") then
  881.             emote = string.sub(msg, 8)
  882.         end
  883.  
  884.         if (pose == "Standing" and emoteNames[emote] ~= nil) then
  885.             playAnimation(emote, EMOTE_TRANSITION_TIME, Humanoid)
  886.         end
  887.     end)
  888.  
  889.     -- emote bindable hook
  890.     script:WaitForChild("PlayEmote").OnInvoke = function(emote)
  891.         -- Only play emotes when idling
  892.         if pose ~= "Standing" then
  893.             return
  894.         end
  895.  
  896.         if emoteNames[emote] ~= nil then
  897.             -- Default emotes
  898.             playAnimation(emote, EMOTE_TRANSITION_TIME, Humanoid)
  899.  
  900.             return true, currentAnimTrack
  901.         elseif typeof(emote) == "Instance" and emote:IsA("Animation") then
  902.             -- Non-default emotes
  903.             playEmote(emote, EMOTE_TRANSITION_TIME, Humanoid)
  904.  
  905.             return true, currentAnimTrack
  906.         end
  907.  
  908.         -- Return false to indicate that the emote could not be played
  909.         return false
  910.     end
  911.  
  912.     if Character.Parent ~= nil then
  913.         -- initialize to idle
  914.         playAnimation("idle", 0.1, Humanoid)
  915.         pose = "Standing"
  916.     end
  917.  
  918.     -- loop to handle timed state transitions and tool animations
  919.     task.spawn(function()
  920.         while Character.Parent ~= nil do
  921.             local _, currentGameTime = wait(0.1)
  922.             stepAnimate(currentGameTime)
  923.         end
  924.     end)
  925. end
  926.  
  927. RunCustomAnimation(plr.Character)
  928.  
  929. plr.CharacterAdded:Connect(function(Char)
  930.     RunCustomAnimation(Char)
  931. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement