Advertisement
w1zlm

re-animate

Jul 2nd, 2023
1,142
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.47 KB | None | 0 0
  1. -- humanoidAnimateR15Moods.lua
  2.  
  3. local Character = game.Players.LocalPlayer.Character
  4. local Humanoid = Character:WaitForChild("Humanoid")
  5. local pose = "Standing"
  6.  
  7. local userNoUpdateOnLoopSuccess, userNoUpdateOnLoopValue = pcall(function() return UserSettings():IsUserFeatureEnabled("UserNoUpdateOnLoop") end)
  8. local userNoUpdateOnLoop = userNoUpdateOnLoopSuccess and userNoUpdateOnLoopValue
  9.  
  10. local userAnimateScaleRunSuccess, userAnimateScaleRunValue = pcall(function() return UserSettings():IsUserFeatureEnabled("UserAnimateScaleRun") end)
  11. local userAnimateScaleRun = userAnimateScaleRunSuccess and userAnimateScaleRunValue
  12.  
  13. local function getRigScale()
  14.     if userAnimateScaleRun then
  15.         return Character:GetScale()
  16.     else
  17.         return 1
  18.     end
  19. end
  20.  
  21. local AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
  22. local HumanoidHipHeight = 2
  23.  
  24. local EMOTE_TRANSITION_TIME = 0.1
  25.  
  26. local currentAnim = ""
  27. local currentAnimInstance = nil
  28. local currentAnimTrack = nil
  29. local currentAnimKeyframeHandler = nil
  30. local currentAnimSpeed = 1.0
  31.  
  32. local runAnimTrack = nil
  33. local runAnimKeyframeHandler = nil
  34.  
  35. local PreloadedAnims = {}
  36.  
  37. local function getAnimTable(anims)
  38.     local returnTable = {}
  39.  
  40.     for _, v in pairs(anims:GetChildren()) do
  41.         if v:IsA("Animation") then
  42.             local AddTable = {id = v.AnimationId, weight = 10}
  43.             if v:FindFirstChild("Weight") then
  44.                 AddTable["Weight"] = v.Weight.Value
  45.             end
  46.  
  47.             returnTable[#returnTable + 1] = AddTable
  48.         end
  49.     end
  50.  
  51.     return returnTable
  52. end
  53.  
  54. local OrigAnimator = Character.Animate
  55. OrigAnimator.Disabled = true
  56.  
  57. local animTable = {}
  58. local animNames = {
  59.     idle =  getAnimTable(OrigAnimator.idle),
  60.     walk =  getAnimTable(OrigAnimator.walk),
  61.     run =   getAnimTable(OrigAnimator.run),
  62.     swim =  getAnimTable(OrigAnimator.swim),
  63.     swimidle =  getAnimTable(OrigAnimator.swimidle),
  64.     jump =  getAnimTable(OrigAnimator.jump),
  65.     fall =  getAnimTable(OrigAnimator.fall),
  66.     climb = getAnimTable(OrigAnimator.climb),
  67.     sit =   getAnimTable(OrigAnimator.sit),
  68.     toolnone =  getAnimTable(OrigAnimator.toolnone),
  69.     toolslash = getAnimTable(OrigAnimator.toolslash),
  70.     toollunge = getAnimTable(OrigAnimator.toollunge),
  71.     wave =  getAnimTable(OrigAnimator.wave),
  72.     point = getAnimTable(OrigAnimator.point),
  73.     dance = getAnimTable(OrigAnimator.dance),
  74.     dance2 =    getAnimTable(OrigAnimator.dance2),
  75.     dance3 =    getAnimTable(OrigAnimator.dance3),
  76.     laugh = getAnimTable(OrigAnimator.laugh),
  77.     cheer = {
  78.                 { id = "http://www.roblox.com/asset/?id=507770677", weight = 10 }
  79.             },
  80. }
  81.  
  82. --[[
  83.     {
  84.         { id = "http://www.roblox.com/asset/?id=507770677", weight = 10 }
  85.     }
  86. ]]
  87.  
  88. -- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
  89. local emoteNames = { wave = false, point = false, dance = true, dance2 = true, dance3 = true, laugh = false, cheer = false}
  90.  
  91. math.randomseed(tick())
  92.  
  93. function findExistingAnimationInSet(set, anim)
  94.     if set == nil or anim == nil then
  95.         return 0
  96.     end
  97.    
  98.     for idx = 1, set.count, 1 do
  99.         if set[idx].anim.AnimationId == anim.AnimationId then
  100.             return idx
  101.         end
  102.     end
  103.    
  104.     return 0
  105. end
  106.  
  107. function configureAnimationSet(name, fileList)
  108.     if (animTable[name] ~= nil) then
  109.         for _, connection in pairs(animTable[name].connections) do
  110.             connection:disconnect()
  111.         end
  112.     end
  113.     animTable[name] = {}
  114.     animTable[name].count = 0
  115.     animTable[name].totalWeight = 0
  116.     animTable[name].connections = {}
  117.  
  118.     local allowCustomAnimations = true
  119.  
  120.     local success, msg = pcall(function() allowCustomAnimations = game:GetService("StarterPlayer").AllowCustomAnimations end)
  121.     if not success then
  122.         allowCustomAnimations = true
  123.     end
  124.  
  125.     -- check for config values
  126.     local config = script:FindFirstChild(name)
  127.     if (allowCustomAnimations and config ~= nil) then
  128.         table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  129.         table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  130.        
  131.         local idx = 0
  132.         for _, childPart in pairs(config:GetChildren()) do
  133.             if (childPart:IsA("Animation")) then
  134.                 local newWeight = 1
  135.                 local weightObject = childPart:FindFirstChild("Weight")
  136.                 if (weightObject ~= nil) then
  137.                     newWeight = weightObject.Value
  138.                 end
  139.                 animTable[name].count = animTable[name].count + 1
  140.                 idx = animTable[name].count
  141.                 animTable[name][idx] = {}
  142.                 animTable[name][idx].anim = childPart
  143.                 animTable[name][idx].weight = newWeight
  144.                 animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  145.                 table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  146.                 table.insert(animTable[name].connections, childPart.ChildAdded:connect(function(property) configureAnimationSet(name, fileList) end))
  147.                 table.insert(animTable[name].connections, childPart.ChildRemoved:connect(function(property) configureAnimationSet(name, fileList) end))
  148.             end
  149.         end
  150.     end
  151.    
  152.     -- fallback to defaults
  153.     if (animTable[name].count <= 0) then
  154.         for idx, anim in pairs(fileList) do
  155.             animTable[name][idx] = {}
  156.             animTable[name][idx].anim = Instance.new("Animation")
  157.             animTable[name][idx].anim.Name = name
  158.             animTable[name][idx].anim.AnimationId = anim.id
  159.             animTable[name][idx].weight = anim.weight
  160.             animTable[name].count = animTable[name].count + 1
  161.             animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  162.         end
  163.     end
  164.    
  165.     -- preload anims
  166.     for i, animType in pairs(animTable) do
  167.         for idx = 1, animType.count, 1 do
  168.             if PreloadedAnims[animType[idx].anim.AnimationId] == nil then
  169.                 Humanoid:LoadAnimation(animType[idx].anim)
  170.                 PreloadedAnims[animType[idx].anim.AnimationId] = true
  171.             end            
  172.         end
  173.     end
  174. end
  175.  
  176. ------------------------------------------------------------------------------------------------------------
  177.  
  178. function configureAnimationSetOld(name, fileList)
  179.     if (animTable[name] ~= nil) then
  180.         for _, connection in pairs(animTable[name].connections) do
  181.             connection:disconnect()
  182.         end
  183.     end
  184.     animTable[name] = {}
  185.     animTable[name].count = 0
  186.     animTable[name].totalWeight = 0
  187.     animTable[name].connections = {}
  188.  
  189.     local allowCustomAnimations = true
  190.  
  191.     local success, msg = pcall(function() allowCustomAnimations = game:GetService("StarterPlayer").AllowCustomAnimations end)
  192.     if not success then
  193.         allowCustomAnimations = true
  194.     end
  195.  
  196.     -- check for config values
  197.     local config = script:FindFirstChild(name)
  198.     if (allowCustomAnimations and config ~= nil) then
  199.         table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  200.         table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  201.         local idx = 1
  202.         for _, childPart in pairs(config:GetChildren()) do
  203.             if (childPart:IsA("Animation")) then
  204.                 table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  205.                 animTable[name][idx] = {}
  206.                 animTable[name][idx].anim = childPart
  207.                 local weightObject = childPart:FindFirstChild("Weight")
  208.                 if (weightObject == nil) then
  209.                     animTable[name][idx].weight = 1
  210.                 else
  211.                     animTable[name][idx].weight = weightObject.Value
  212.                 end
  213.                 animTable[name].count = animTable[name].count + 1
  214.                 animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  215.                 idx = idx + 1
  216.             end
  217.         end
  218.     end
  219.  
  220.     -- fallback to defaults
  221.     if (animTable[name].count <= 0) then
  222.         for idx, anim in pairs(fileList) do
  223.             animTable[name][idx] = {}
  224.             animTable[name][idx].anim = Instance.new("Animation")
  225.             animTable[name][idx].anim.Name = name
  226.             animTable[name][idx].anim.AnimationId = anim.id
  227.             animTable[name][idx].weight = anim.weight
  228.             animTable[name].count = animTable[name].count + 1
  229.             animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  230.             -- print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
  231.         end
  232.     end
  233.    
  234.     -- preload anims
  235.     for i, animType in pairs(animTable) do
  236.         for idx = 1, animType.count, 1 do
  237.             Humanoid:LoadAnimation(animType[idx].anim)
  238.         end
  239.     end
  240. end
  241.  
  242. -- Setup animation objects
  243. function scriptChildModified(child)
  244.     local fileList = animNames[child.Name]
  245.     if (fileList ~= nil) then
  246.         configureAnimationSet(child.Name, fileList)
  247.     end
  248. end
  249.  
  250. script.ChildAdded:connect(scriptChildModified)
  251. script.ChildRemoved:connect(scriptChildModified)
  252.  
  253. -- Clear any existing animation tracks
  254. -- Fixes issue with characters that are moved in and out of the Workspace accumulating tracks
  255. local animator = if Humanoid then Humanoid:FindFirstChildOfClass("Animator") else nil
  256. if animator then
  257.     local animTracks = animator:GetPlayingAnimationTracks()
  258.     for i,track in ipairs(animTracks) do
  259.         track:Stop(0)
  260.         track:Destroy()
  261.     end
  262. end
  263.  
  264. for name, fileList in pairs(animNames) do
  265.     configureAnimationSet(name, fileList)
  266. end
  267.  
  268. -- ANIMATION
  269.  
  270. -- declarations
  271. local toolAnim = "None"
  272. local toolAnimTime = 0
  273.  
  274. local jumpAnimTime = 0
  275. local jumpAnimDuration = 0.31
  276.  
  277. local toolTransitionTime = 0.1
  278. local fallTransitionTime = 0.2
  279.  
  280. local currentlyPlayingEmote = false
  281.  
  282. -- functions
  283.  
  284. function stopAllAnimations()
  285.     local oldAnim = currentAnim
  286.  
  287.     -- return to idle if finishing an emote
  288.     if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  289.         oldAnim = "idle"
  290.     end
  291.    
  292.     if currentlyPlayingEmote then
  293.         oldAnim = "idle"
  294.         currentlyPlayingEmote = false
  295.     end
  296.  
  297.     currentAnim = ""
  298.     currentAnimInstance = nil
  299.     if (currentAnimKeyframeHandler ~= nil) then
  300.         currentAnimKeyframeHandler:disconnect()
  301.     end
  302.  
  303.     if (currentAnimTrack ~= nil) then
  304.         currentAnimTrack:Stop()
  305.         currentAnimTrack:Destroy()
  306.         currentAnimTrack = nil
  307.     end
  308.  
  309.     -- clean up walk if there is one
  310.     if (runAnimKeyframeHandler ~= nil) then
  311.         runAnimKeyframeHandler:disconnect()
  312.     end
  313.    
  314.     if (runAnimTrack ~= nil) then
  315.         runAnimTrack:Stop()
  316.         runAnimTrack:Destroy()
  317.         runAnimTrack = nil
  318.     end
  319.    
  320.     return oldAnim
  321. end
  322.  
  323. function getHeightScale()
  324.     if Humanoid then
  325.         if not Humanoid.AutomaticScalingEnabled then
  326.             -- When auto scaling is not enabled, the rig scale stands in for
  327.             -- a computed scale.
  328.             return getRigScale()
  329.         end
  330.        
  331.         local scale = Humanoid.HipHeight / HumanoidHipHeight
  332.         if AnimationSpeedDampeningObject == nil then
  333.             AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
  334.         end
  335.         if AnimationSpeedDampeningObject ~= nil then
  336.             scale = 1 + (Humanoid.HipHeight - HumanoidHipHeight) * AnimationSpeedDampeningObject.Value / HumanoidHipHeight
  337.         end
  338.         return scale
  339.     end
  340.     return getRigScale()
  341. end
  342.  
  343. local function rootMotionCompensation(speed)
  344.     local speedScaled = speed * 1.25
  345.     local heightScale = getHeightScale()
  346.     local runSpeed = speedScaled / heightScale
  347.     return runSpeed
  348. end
  349.  
  350. local smallButNotZero = 0.0001
  351. local function setRunSpeed(speed)
  352.     local normalizedWalkSpeed = 0.5 -- established empirically using current `913402848` walk animation
  353.     local normalizedRunSpeed  = 1
  354.     local runSpeed = rootMotionCompensation(speed)
  355.  
  356.     local walkAnimationWeight = smallButNotZero
  357.     local runAnimationWeight = smallButNotZero
  358.     local walkAnimationTimewarp = runSpeed/normalizedWalkSpeed
  359.     local runAnimationTimerwarp = runSpeed/normalizedRunSpeed
  360.  
  361.     if runSpeed <= normalizedWalkSpeed then
  362.         walkAnimationWeight = 1
  363.     elseif runSpeed < normalizedRunSpeed then
  364.         local fadeInRun = (runSpeed - normalizedWalkSpeed)/(normalizedRunSpeed - normalizedWalkSpeed)
  365.         walkAnimationWeight = 1 - fadeInRun
  366.         runAnimationWeight  = fadeInRun
  367.         walkAnimationTimewarp = 1
  368.         runAnimationTimerwarp = 1
  369.     else
  370.         runAnimationWeight = 1
  371.     end
  372.     currentAnimTrack:AdjustWeight(walkAnimationWeight)
  373.     runAnimTrack:AdjustWeight(runAnimationWeight)
  374.     currentAnimTrack:AdjustSpeed(walkAnimationTimewarp)
  375.     runAnimTrack:AdjustSpeed(runAnimationTimerwarp)
  376. end
  377.  
  378. function setAnimationSpeed(speed)
  379.     if currentAnim == "walk" then
  380.             setRunSpeed(speed)
  381.     else
  382.         if speed ~= currentAnimSpeed then
  383.             currentAnimSpeed = speed
  384.             currentAnimTrack:AdjustSpeed(currentAnimSpeed)
  385.         end
  386.     end
  387. end
  388.  
  389. function keyFrameReachedFunc(frameName)
  390.     if (frameName == "End") then
  391.         if currentAnim == "walk" then
  392.             if userNoUpdateOnLoop == true then
  393.                 if runAnimTrack.Looped ~= true then
  394.                     runAnimTrack.TimePosition = 0.0
  395.                 end
  396.                 if currentAnimTrack.Looped ~= true then
  397.                     currentAnimTrack.TimePosition = 0.0
  398.                 end
  399.             else
  400.                 runAnimTrack.TimePosition = 0.0
  401.                 currentAnimTrack.TimePosition = 0.0
  402.             end
  403.         else
  404.             local repeatAnim = currentAnim
  405.             -- return to idle if finishing an emote
  406.             if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
  407.                 repeatAnim = "idle"
  408.             end
  409.            
  410.             if currentlyPlayingEmote then
  411.                 if currentAnimTrack.Looped then
  412.                     -- Allow the emote to loop
  413.                     return
  414.                 end
  415.                
  416.                 repeatAnim = "idle"
  417.                 currentlyPlayingEmote = false
  418.             end
  419.            
  420.             local animSpeed = currentAnimSpeed
  421.             playAnimation(repeatAnim, 0.15, Humanoid)
  422.             setAnimationSpeed(animSpeed)
  423.         end
  424.     end
  425. end
  426.  
  427. function rollAnimation(animName)
  428.     local roll = math.random(1, animTable[animName].totalWeight)
  429.     local origRoll = roll
  430.     local idx = 1
  431.     while (roll > animTable[animName][idx].weight) do
  432.         roll = roll - animTable[animName][idx].weight
  433.         idx = idx + 1
  434.     end
  435.     return idx
  436. end
  437.  
  438. local function switchToAnim(anim, animName, transitionTime, humanoid)
  439.     -- switch animation    
  440.     if (anim ~= currentAnimInstance) then
  441.        
  442.         if (currentAnimTrack ~= nil) then
  443.             currentAnimTrack:Stop(transitionTime)
  444.             currentAnimTrack:Destroy()
  445.         end
  446.  
  447.         if (runAnimTrack ~= nil) then
  448.             runAnimTrack:Stop(transitionTime)
  449.             runAnimTrack:Destroy()
  450.             if userNoUpdateOnLoop == true then
  451.                 runAnimTrack = nil
  452.             end
  453.         end
  454.  
  455.         currentAnimSpeed = 1.0
  456.    
  457.         -- load it to the humanoid; get AnimationTrack
  458.         currentAnimTrack = humanoid:LoadAnimation(anim)
  459.         currentAnimTrack.Priority = Enum.AnimationPriority.Core
  460.          
  461.         -- play the animation
  462.         currentAnimTrack:Play(transitionTime)
  463.         currentAnim = animName
  464.         currentAnimInstance = anim
  465.  
  466.         -- set up keyframe name triggers
  467.         if (currentAnimKeyframeHandler ~= nil) then
  468.             currentAnimKeyframeHandler:disconnect()
  469.         end
  470.         currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  471.        
  472.         -- check to see if we need to blend a walk/run animation
  473.         if animName == "walk" then
  474.             local runAnimName = "run"
  475.             local runIdx = rollAnimation(runAnimName)
  476.  
  477.             runAnimTrack = humanoid:LoadAnimation(animTable[runAnimName][runIdx].anim)
  478.             runAnimTrack.Priority = Enum.AnimationPriority.Core
  479.             runAnimTrack:Play(transitionTime)      
  480.            
  481.             if (runAnimKeyframeHandler ~= nil) then
  482.                 runAnimKeyframeHandler:disconnect()
  483.             end
  484.             runAnimKeyframeHandler = runAnimTrack.KeyframeReached:connect(keyFrameReachedFunc) 
  485.         end
  486.     end
  487. end
  488.  
  489. function playAnimation(animName, transitionTime, humanoid)  
  490.     local idx = rollAnimation(animName)
  491.     local anim = animTable[animName][idx].anim
  492.  
  493.     switchToAnim(anim, animName, transitionTime, humanoid)
  494.     currentlyPlayingEmote = false
  495. end
  496.  
  497. function playEmote(emoteAnim, transitionTime, humanoid)
  498.     switchToAnim(emoteAnim, emoteAnim.Name, transitionTime, humanoid)
  499.     currentlyPlayingEmote = true
  500. end
  501.  
  502. -------------------------------------------------------------------------------------------
  503. -------------------------------------------------------------------------------------------
  504.  
  505. local toolAnimName = ""
  506. local toolAnimTrack = nil
  507. local toolAnimInstance = nil
  508. local currentToolAnimKeyframeHandler = nil
  509.  
  510. function toolKeyFrameReachedFunc(frameName)
  511.     if (frameName == "End") then
  512.         playToolAnimation(toolAnimName, 0.0, Humanoid)
  513.     end
  514. end
  515.  
  516.  
  517. function playToolAnimation(animName, transitionTime, humanoid, priority)           
  518.         local idx = rollAnimation(animName)
  519.         local anim = animTable[animName][idx].anim
  520.  
  521.         if (toolAnimInstance ~= anim) then
  522.            
  523.             if (toolAnimTrack ~= nil) then
  524.                 toolAnimTrack:Stop()
  525.                 toolAnimTrack:Destroy()
  526.                 transitionTime = 0
  527.             end
  528.                    
  529.             -- load it to the humanoid; get AnimationTrack
  530.             toolAnimTrack = humanoid:LoadAnimation(anim)
  531.             if priority then
  532.                 toolAnimTrack.Priority = priority
  533.             end
  534.              
  535.             -- play the animation
  536.             toolAnimTrack:Play(transitionTime)
  537.             toolAnimName = animName
  538.             toolAnimInstance = anim
  539.  
  540.             currentToolAnimKeyframeHandler = toolAnimTrack.KeyframeReached:connect(toolKeyFrameReachedFunc)
  541.         end
  542. end
  543.  
  544. function stopToolAnimations()
  545.     local oldAnim = toolAnimName
  546.  
  547.     if (currentToolAnimKeyframeHandler ~= nil) then
  548.         currentToolAnimKeyframeHandler:disconnect()
  549.     end
  550.  
  551.     toolAnimName = ""
  552.     toolAnimInstance = nil
  553.     if (toolAnimTrack ~= nil) then
  554.         toolAnimTrack:Stop()
  555.         toolAnimTrack:Destroy()
  556.         toolAnimTrack = nil
  557.     end
  558.  
  559.     return oldAnim
  560. end
  561.  
  562. -------------------------------------------------------------------------------------------
  563. -------------------------------------------------------------------------------------------
  564. -- STATE CHANGE HANDLERS
  565.  
  566. function onRunning(speed)
  567.     local heightScale = if userAnimateScaleRun then getHeightScale() else 1
  568.    
  569.     local movedDuringEmote = currentlyPlayingEmote and Humanoid.MoveDirection == Vector3.new(0, 0, 0)
  570.     local speedThreshold = movedDuringEmote and (Humanoid.WalkSpeed / heightScale) or 0.75
  571.     if speed > speedThreshold * heightScale then
  572.         local scale = 16.0
  573.         playAnimation("walk", 0.2, Humanoid)
  574.         setAnimationSpeed(speed / scale)
  575.         pose = "Running"
  576.     else
  577.         if emoteNames[currentAnim] == nil and not currentlyPlayingEmote then
  578.             playAnimation("idle", 0.2, Humanoid)
  579.             pose = "Standing"
  580.         end
  581.     end
  582. end
  583.  
  584. function onDied()
  585.     pose = "Dead"
  586. end
  587.  
  588. function onJumping()
  589.     playAnimation("jump", 0.1, Humanoid)
  590.     jumpAnimTime = jumpAnimDuration
  591.     pose = "Jumping"
  592. end
  593.  
  594. function onClimbing(speed)
  595.     if userAnimateScaleRun then
  596.         speed /= getHeightScale()
  597.     end
  598.     local scale = 5.0
  599.     playAnimation("climb", 0.1, Humanoid)
  600.     setAnimationSpeed(speed / scale)
  601.     pose = "Climbing"
  602. end
  603.  
  604. function onGettingUp()
  605.     pose = "GettingUp"
  606. end
  607.  
  608. function onFreeFall()
  609.     if (jumpAnimTime <= 0) then
  610.         playAnimation("fall", fallTransitionTime, Humanoid)
  611.     end
  612.     pose = "FreeFall"
  613. end
  614.  
  615. function onFallingDown()
  616.     pose = "FallingDown"
  617. end
  618.  
  619. function onSeated()
  620.     pose = "Seated"
  621. end
  622.  
  623. function onPlatformStanding()
  624.     pose = "PlatformStanding"
  625. end
  626.  
  627. -------------------------------------------------------------------------------------------
  628. -------------------------------------------------------------------------------------------
  629.  
  630. function onSwimming(speed)
  631.     if userAnimateScaleRun then
  632.         speed /= getHeightScale()
  633.     end
  634.     if speed > 1.00 then
  635.         local scale = 10.0
  636.         playAnimation("swim", 0.4, Humanoid)
  637.         setAnimationSpeed(speed / scale)
  638.         pose = "Swimming"
  639.     else
  640.         playAnimation("swimidle", 0.4, Humanoid)
  641.         pose = "Standing"
  642.     end
  643. end
  644.  
  645. function animateTool()
  646.     if (toolAnim == "None") then
  647.         playToolAnimation("toolnone", toolTransitionTime, Humanoid, Enum.AnimationPriority.Idle)
  648.         return
  649.     end
  650.  
  651.     if (toolAnim == "Slash") then
  652.         playToolAnimation("toolslash", 0, Humanoid, Enum.AnimationPriority.Action)
  653.         return
  654.     end
  655.  
  656.     if (toolAnim == "Lunge") then
  657.         playToolAnimation("toollunge", 0, Humanoid, Enum.AnimationPriority.Action)
  658.         return
  659.     end
  660. end
  661.  
  662. function getToolAnim(tool)
  663.     for _, c in ipairs(tool:GetChildren()) do
  664.         if c.Name == "toolanim" and c.className == "StringValue" then
  665.             return c
  666.         end
  667.     end
  668.     return nil
  669. end
  670.  
  671. local lastTick = 0
  672.  
  673. function stepAnimate(currentTime)
  674.     local amplitude = 1
  675.     local frequency = 1
  676.     local deltaTime = currentTime - lastTick
  677.     lastTick = currentTime
  678.  
  679.     local climbFudge = 0
  680.     local setAngles = false
  681.  
  682.     if (jumpAnimTime > 0) then
  683.         jumpAnimTime = jumpAnimTime - deltaTime
  684.     end
  685.  
  686.     if (pose == "FreeFall" and jumpAnimTime <= 0) then
  687.         playAnimation("fall", fallTransitionTime, Humanoid)
  688.     elseif (pose == "Seated") then
  689.         playAnimation("sit", 0.5, Humanoid)
  690.         return
  691.     elseif (pose == "Running") then
  692.         playAnimation("walk", 0.2, Humanoid)
  693.     elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  694.         stopAllAnimations()
  695.         amplitude = 0.1
  696.         frequency = 1
  697.         setAngles = true
  698.     end
  699.  
  700.     -- Tool Animation handling
  701.     local tool = Character:FindFirstChildOfClass("Tool")
  702.     if tool and tool:FindFirstChild("Handle") then
  703.         local animStringValueObject = getToolAnim(tool)
  704.  
  705.         if animStringValueObject then
  706.             toolAnim = animStringValueObject.Value
  707.             -- message recieved, delete StringValue
  708.             animStringValueObject.Parent = nil
  709.             toolAnimTime = currentTime + .3
  710.         end
  711.  
  712.         if currentTime > toolAnimTime then
  713.             toolAnimTime = 0
  714.             toolAnim = "None"
  715.         end
  716.  
  717.         animateTool()      
  718.     else
  719.         stopToolAnimations()
  720.         toolAnim = "None"
  721.         toolAnimInstance = nil
  722.         toolAnimTime = 0
  723.     end
  724. end
  725.  
  726. -- connect events
  727. Humanoid.Died:connect(onDied)
  728. Humanoid.Running:connect(onRunning)
  729. Humanoid.Jumping:connect(onJumping)
  730. Humanoid.Climbing:connect(onClimbing)
  731. Humanoid.GettingUp:connect(onGettingUp)
  732. Humanoid.FreeFalling:connect(onFreeFall)
  733. Humanoid.FallingDown:connect(onFallingDown)
  734. Humanoid.Seated:connect(onSeated)
  735. Humanoid.PlatformStanding:connect(onPlatformStanding)
  736. Humanoid.Swimming:connect(onSwimming)
  737.  
  738. -- setup emote chat hook
  739. game:GetService("Players").LocalPlayer.Chatted:connect(function(msg)
  740.     local emote = ""
  741.     if (string.sub(msg, 1, 3) == "/e ") then
  742.         emote = string.sub(msg, 4)
  743.     elseif (string.sub(msg, 1, 7) == "/emote ") then
  744.         emote = string.sub(msg, 8)
  745.     end
  746.    
  747.     if (pose == "Standing" and emoteNames[emote] ~= nil) then
  748.         playAnimation(emote, EMOTE_TRANSITION_TIME, Humanoid)
  749.     end
  750. end)
  751.  
  752. -- emote bindable hook
  753. script:WaitForChild("PlayEmote").OnInvoke = function(emote)
  754.     -- Only play emotes when idling
  755.     if pose ~= "Standing" then
  756.         return
  757.     end
  758.  
  759.     if emoteNames[emote] ~= nil then
  760.         -- Default emotes
  761.         playAnimation(emote, EMOTE_TRANSITION_TIME, Humanoid)
  762.        
  763.         return true, currentAnimTrack
  764.     elseif typeof(emote) == "Instance" and emote:IsA("Animation") then
  765.         -- Non-default emotes
  766.         playEmote(emote, EMOTE_TRANSITION_TIME, Humanoid)
  767.  
  768.         return true, currentAnimTrack
  769.     end
  770.    
  771.     -- Return false to indicate that the emote could not be played
  772.     return false
  773. end
  774.  
  775. if Character.Parent ~= nil then
  776.     -- initialize to idle
  777.     playAnimation("idle", 0.1, Humanoid)
  778.     pose = "Standing"
  779. end
  780.  
  781. -- loop to handle timed state transitions and tool animations
  782. while Character.Parent ~= nil do
  783.     local _, currentGameTime = wait(0.1)
  784.     stepAnimate(currentGameTime)
  785. end
  786.  
  787.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement