SHARE
TWEET

Untitled

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