Guest User

Untitled

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