Advertisement
RobloxDel

Npc Animation

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