DrawingJhon

Animate script

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