Mryeetmemes

Roblox Animate (script inside you when you join a roblox game)

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