Advertisement
ERROR_CODE

.R15toR6

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