DrawingJhon

Animate Script

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