Advertisement
qsenko1

Animation Script

Apr 30th, 2022
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.68 KB | None | 0 0
  1. local Figure = script.Parent
  2. local Torso = Figure:WaitForChild("Torso")
  3. local RightShoulder = Torso:WaitForChild("Right Shoulder")
  4. local LeftShoulder = Torso:WaitForChild("Left Shoulder")
  5. local RightHip = Torso:WaitForChild("Right Hip")
  6. local LeftHip = Torso:WaitForChild("Left Hip")
  7. local Neck = Torso:WaitForChild("Neck")
  8. local Humanoid = Figure:WaitForChild("Humanoid")
  9. local pose = "Standing"
  10.  
  11. local currentAnim = ""
  12. local currentAnimInstance = nil
  13. local currentAnimTrack = nil
  14. local currentAnimKeyframeHandler = nil
  15. local currentAnimSpeed = 1.0
  16. local animTable = {}
  17. local animNames = {
  18.     idle =  {  
  19.                 { id = "http://www.roblox.com/asset/?id=180435571", weight = 9 },
  20.                 { id = "http://www.roblox.com/asset/?id=180435792", weight = 1 }
  21.             },
  22.     walk =  {  
  23.                 { id = "http://www.roblox.com/asset/?id=180426354", weight = 10 }
  24.             },
  25.     run =   {
  26.                 { id = "run.xml", weight = 10 }
  27.             },
  28.     jump =  {
  29.         --{ id = "http://www.roblox.com/asset/?id=125750702", weight = 10 }
  30.                 { id = "http://www.roblox.com/asset/?id=9507631983", weight = 11 }
  31.             },
  32.     fall =  {
  33.                 { id = "http://www.roblox.com/asset/?id=180436148", weight = 10 }
  34.             },
  35.     climb = {
  36.                 { id = "http://www.roblox.com/asset/?id=180436334", weight = 10 }
  37.             },
  38.     sit =   {
  39.                 { id = "http://www.roblox.com/asset/?id=178130996", weight = 10 }
  40.             }, 
  41.     toolnone = {
  42.                 { id = "http://www.roblox.com/asset/?id=182393478", weight = 10 }
  43.             },
  44.     toolslash = {
  45.                 { id = "http://www.roblox.com/asset/?id=129967390", weight = 10 }
  46. --              { id = "slash.xml", weight = 10 }
  47.             },
  48.     toollunge = {
  49.                 { id = "http://www.roblox.com/asset/?id=129967478", weight = 10 }
  50.             },
  51.     wave = {
  52.                 { id = "http://www.roblox.com/asset/?id=128777973", weight = 10 }
  53.             },
  54.     point = {
  55.                 { id = "http://www.roblox.com/asset/?id=128853357", weight = 10 }
  56.             },
  57.     dance1 = {
  58.                 { id = "http://www.roblox.com/asset/?id=182435998", weight = 10 },
  59.                 { id = "http://www.roblox.com/asset/?id=182491037", weight = 10 },
  60.                 { id = "http://www.roblox.com/asset/?id=182491065", weight = 10 }
  61.             },
  62.     dance2 = {
  63.                 { id = "http://www.roblox.com/asset/?id=182436842", weight = 10 },
  64.                 { id = "http://www.roblox.com/asset/?id=182491248", weight = 10 },
  65.                 { id = "http://www.roblox.com/asset/?id=182491277", weight = 10 }
  66.             },
  67.     dance3 = {
  68.                 { id = "http://www.roblox.com/asset/?id=182436935", weight = 10 },
  69.                 { id = "http://www.roblox.com/asset/?id=182491368", weight = 10 },
  70.                 { id = "http://www.roblox.com/asset/?id=182491423", weight = 10 }
  71.             },
  72.     laugh = {
  73.                 { id = "http://www.roblox.com/asset/?id=129423131", weight = 10 }
  74.             },
  75.     cheer = {
  76.                 { id = "http://www.roblox.com/asset/?id=129423030", weight = 10 }
  77.             },
  78. }
  79. local dances = {"dance1", "dance2", "dance3"}
  80.  
  81. -- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
  82. local emoteNames = { wave = false, point = false, dance1 = true, dance2 = true, dance3 = true, laugh = false, cheer = false}
  83.  
  84. function configureAnimationSet(name, fileList)
  85.     if (animTable[name] ~= nil) then
  86.         for _, connection in pairs(animTable[name].connections) do
  87.             connection:disconnect()
  88.         end
  89.     end
  90.     animTable[name] = {}
  91.     animTable[name].count = 0
  92.     animTable[name].totalWeight = 0
  93.     animTable[name].connections = {}
  94.  
  95.     -- check for config values
  96.     local config = script:FindFirstChild(name)
  97.     if (config ~= nil) then
  98. --      print("Loading anims " .. name)
  99.         table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  100.         table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  101.         local idx = 1
  102.         for _, childPart in pairs(config:GetChildren()) do
  103.             if (childPart:IsA("Animation")) then
  104.                 table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  105.                 animTable[name][idx] = {}
  106.                 animTable[name][idx].anim = childPart
  107.                 local weightObject = childPart:FindFirstChild("Weight")
  108.                 if (weightObject == nil) then
  109.                     animTable[name][idx].weight = 1
  110.                 else
  111.                     animTable[name][idx].weight = weightObject.Value
  112.                 end
  113.                 animTable[name].count = animTable[name].count + 1
  114.                 animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  115.     --          print(name .. " [" .. idx .. "] " .. animTable[name][idx].anim.AnimationId .. " (" .. animTable[name][idx].weight .. ")")
  116.                 idx = idx + 1
  117.             end
  118.         end
  119.     end
  120.  
  121.     -- fallback to defaults
  122.     if (animTable[name].count <= 0) then
  123.         for idx, anim in pairs(fileList) do
  124.             animTable[name][idx] = {}
  125.             animTable[name][idx].anim = Instance.new("Animation")
  126.             animTable[name][idx].anim.Name = name
  127.             animTable[name][idx].anim.AnimationId = anim.id
  128.             animTable[name][idx].weight = anim.weight
  129.             animTable[name].count = animTable[name].count + 1
  130.             animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  131. --          print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
  132.         end
  133.     end
  134. end
  135.  
  136. -- Setup animation objects
  137. function scriptChildModified(child)
  138.     local fileList = animNames[child.Name]
  139.     if (fileList ~= nil) then
  140.         configureAnimationSet(child.Name, fileList)
  141.     end
  142. end
  143.  
  144. script.ChildAdded:connect(scriptChildModified)
  145. script.ChildRemoved:connect(scriptChildModified)
  146.  
  147.  
  148. for name, fileList in pairs(animNames) do
  149.     configureAnimationSet(name, fileList)
  150. end
  151.  
  152. -- ANIMATION
  153.  
  154. -- declarations
  155. local toolAnim = "None"
  156. local toolAnimTime = 0
  157.  
  158. local jumpAnimTime = 0--1.08--0
  159. local jumpAnimDuration = 1.08--0.3
  160.  
  161. local toolTransitionTime = 0.1
  162. local fallTransitionTime =  1.08--2.08 --0.3
  163. local jumpMaxLimbVelocity = 0.75
  164.  
  165. -- functions
  166.  
  167. function stopAllAnimations()
  168.     local oldAnim = currentAnim
  169.  
  170.     -- return to idle if finishing an emote
  171.     if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  172.         oldAnim = "idle"
  173.     end
  174.  
  175.     currentAnim = ""
  176.     currentAnimInstance = nil
  177.     if (currentAnimKeyframeHandler ~= nil) then
  178.         currentAnimKeyframeHandler:disconnect()
  179.     end
  180.  
  181.     if (currentAnimTrack ~= nil) then
  182.         currentAnimTrack:Stop()
  183.         currentAnimTrack:Destroy()
  184.         currentAnimTrack = nil
  185.     end
  186.     return oldAnim
  187. end
  188.  
  189. function setAnimationSpeed(speed)
  190.     if speed ~= currentAnimSpeed then
  191.         currentAnimSpeed = speed
  192.         currentAnimTrack:AdjustSpeed(currentAnimSpeed)
  193.     end
  194. end
  195.  
  196. function keyFrameReachedFunc(frameName)
  197.     if (frameName == "End") then
  198.  
  199.         local repeatAnim = currentAnim
  200.         -- return to idle if finishing an emote
  201.         if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
  202.             repeatAnim = "idle"
  203.         end
  204.        
  205.         local animSpeed = currentAnimSpeed
  206.         playAnimation(repeatAnim, 1, Humanoid)
  207.         setAnimationSpeed(animSpeed)
  208.     end
  209. end
  210.  
  211. -- Preload animations
  212. function playAnimation(animName, transitionTime, humanoid)
  213.        
  214.     local roll = math.random(1, animTable[animName].totalWeight)
  215.     local origRoll = roll
  216.     local idx = 1
  217.     while (roll > animTable[animName][idx].weight) do
  218.         roll = roll - animTable[animName][idx].weight
  219.         idx = idx + 1
  220.     end
  221. --      print(animName .. " " .. idx .. " [" .. origRoll .. "]")
  222.     local anim = animTable[animName][idx].anim
  223.  
  224.     -- switch animation    
  225.     if (anim ~= currentAnimInstance) then
  226.        
  227.         if (currentAnimTrack ~= nil) then
  228.             currentAnimTrack:Stop(transitionTime)
  229.             currentAnimTrack:Destroy()
  230.         end
  231.  
  232.         currentAnimSpeed = 1.0
  233.    
  234.         -- load it to the humanoid; get AnimationTrack
  235.         currentAnimTrack = humanoid:LoadAnimation(anim)
  236.         currentAnimTrack.Priority = Enum.AnimationPriority.Core
  237.          
  238.         -- play the animation
  239.         currentAnimTrack:Play(transitionTime)
  240.         currentAnim = animName
  241.         currentAnimInstance = anim
  242.  
  243.         -- set up keyframe name triggers
  244.         if (currentAnimKeyframeHandler ~= nil) then
  245.             currentAnimKeyframeHandler:disconnect()
  246.         end
  247.         currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  248.        
  249.     end
  250.  
  251. end
  252.  
  253. -------------------------------------------------------------------------------------------
  254. -------------------------------------------------------------------------------------------
  255.  
  256. local toolAnimName = ""
  257. local toolAnimTrack = nil
  258. local toolAnimInstance = nil
  259. local currentToolAnimKeyframeHandler = nil
  260.  
  261. function toolKeyFrameReachedFunc(frameName)
  262.     if (frameName == "End") then
  263. --      print("Keyframe : ".. frameName)   
  264.         playToolAnimation(toolAnimName, 0.0, Humanoid)
  265.     end
  266. end
  267.  
  268.  
  269. function playToolAnimation(animName, transitionTime, humanoid, priority)     
  270.        
  271.         local roll = math.random(1, animTable[animName].totalWeight)
  272.         local origRoll = roll
  273.         local idx = 1
  274.         while (roll > animTable[animName][idx].weight) do
  275.             roll = roll - animTable[animName][idx].weight
  276.             idx = idx + 1
  277.         end
  278. --      print(animName .. " * " .. idx .. " [" .. origRoll .. "]")
  279.         local anim = animTable[animName][idx].anim
  280.  
  281.         if (toolAnimInstance ~= anim) then
  282.            
  283.             if (toolAnimTrack ~= nil) then
  284.                 toolAnimTrack:Stop()
  285.                 toolAnimTrack:Destroy()
  286.                 transitionTime = 0
  287.             end
  288.                    
  289.             -- load it to the humanoid; get AnimationTrack
  290.             toolAnimTrack = humanoid:LoadAnimation(anim)
  291.             if priority then
  292.                 toolAnimTrack.Priority = priority
  293.             end
  294.              
  295.             -- play the animation
  296.             toolAnimTrack:Play(transitionTime)
  297.             toolAnimName = animName
  298.             toolAnimInstance = anim
  299.  
  300.             currentToolAnimKeyframeHandler = toolAnimTrack.KeyframeReached:connect(toolKeyFrameReachedFunc)
  301.         end
  302. end
  303.  
  304. function stopToolAnimations()
  305.     local oldAnim = toolAnimName
  306.  
  307.     if (currentToolAnimKeyframeHandler ~= nil) then
  308.         currentToolAnimKeyframeHandler:disconnect()
  309.     end
  310.  
  311.     toolAnimName = ""
  312.     toolAnimInstance = nil
  313.     if (toolAnimTrack ~= nil) then
  314.         toolAnimTrack:Stop()
  315.         toolAnimTrack:Destroy()
  316.         toolAnimTrack = nil
  317.     end
  318.  
  319.  
  320.     return oldAnim
  321. end
  322.  
  323. -------------------------------------------------------------------------------------------
  324. -------------------------------------------------------------------------------------------
  325.  
  326.  
  327. function onRunning(speed)
  328.     if speed > 0.01 then
  329.         playAnimation("walk", 0.1, Humanoid)
  330.         if currentAnimInstance and currentAnimInstance.AnimationId == "http://www.roblox.com/asset/?id=180426354" then
  331.             setAnimationSpeed(speed / 14.5)
  332.         end
  333.         pose = "Running"
  334.     else
  335.         if emoteNames[currentAnim] == nil then
  336.             playAnimation("idle", 0.1, Humanoid)
  337.             pose = "Standing"
  338.         end
  339.     end
  340. end
  341.  
  342. function onDied()
  343.     pose = "Dead"
  344. end
  345.  
  346. function onJumping()
  347.     playAnimation("jump",0.1 ,Humanoid)--0.1,
  348.     jumpAnimTime = jumpAnimDuration
  349.     pose = "Jumping"
  350. end
  351.  
  352. function onClimbing(speed)
  353.     playAnimation("climb", 0.1, Humanoid)
  354.     setAnimationSpeed(speed / 12.0)
  355.     pose = "Climbing"
  356. end
  357.  
  358. function onGettingUp()
  359.     pose = "GettingUp"
  360. end
  361.  
  362. function onFreeFall()
  363.     if (jumpAnimTime <= 0) then
  364.         playAnimation("fall", fallTransitionTime, Humanoid)
  365.     end
  366.     pose = "FreeFall"
  367. end
  368.  
  369. function onFallingDown()
  370.     pose = "FallingDown"
  371. end
  372.  
  373. function onSeated()
  374.     pose = "Seated"
  375. end
  376.  
  377. function onPlatformStanding()
  378.     pose = "PlatformStanding"
  379. end
  380.  
  381. function onSwimming(speed)
  382.     if speed > 0 then
  383.         pose = "Running"
  384.     else
  385.         pose = "Standing"
  386.     end
  387. end
  388.  
  389. function getTool() 
  390.     for _, kid in ipairs(Figure:GetChildren()) do
  391.         if kid.className == "Tool" then return kid end
  392.     end
  393.     return nil
  394. end
  395.  
  396. function getToolAnim(tool)
  397.     for _, c in ipairs(tool:GetChildren()) do
  398.         if c.Name == "toolanim" and c.className == "StringValue" then
  399.             return c
  400.         end
  401.     end
  402.     return nil
  403. end
  404.  
  405. function animateTool()
  406.    
  407.     if (toolAnim == "None") then
  408.         playToolAnimation("toolnone", toolTransitionTime, Humanoid, Enum.AnimationPriority.Idle)
  409.         return
  410.     end
  411.  
  412.     if (toolAnim == "Slash") then
  413.         playToolAnimation("toolslash", 0, Humanoid, Enum.AnimationPriority.Action)
  414.         return
  415.     end
  416.  
  417.     if (toolAnim == "Lunge") then
  418.         playToolAnimation("toollunge", 0, Humanoid, Enum.AnimationPriority.Action)
  419.         return
  420.     end
  421. end
  422.  
  423. function moveSit()
  424.     RightShoulder.MaxVelocity = 0.15
  425.     LeftShoulder.MaxVelocity = 0.15
  426.     RightShoulder:SetDesiredAngle(3.14 /2)
  427.     LeftShoulder:SetDesiredAngle(-3.14 /2)
  428.     RightHip:SetDesiredAngle(3.14 /2)
  429.     LeftHip:SetDesiredAngle(-3.14 /2)
  430. end
  431.  
  432. local lastTick = 0
  433.  
  434. function move(time)
  435.     local amplitude = 1
  436.     local frequency = 1
  437.     local deltaTime = time - lastTick
  438.     lastTick = time
  439.  
  440.     local climbFudge = 0
  441.     local setAngles = false
  442.  
  443.     if (jumpAnimTime > 0) then
  444.         jumpAnimTime = jumpAnimTime - deltaTime
  445.     end
  446.  
  447.     if (pose == "FreeFall" and jumpAnimTime <= 0) then
  448.         playAnimation("fall", fallTransitionTime, Humanoid)
  449.     elseif (pose == "Seated") then
  450.         playAnimation("sit", 0.5, Humanoid)
  451.         return
  452.     elseif (pose == "Running") then
  453.         playAnimation("walk", 0.1, Humanoid)
  454.     elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  455. --      print("Wha " .. pose)
  456.         stopAllAnimations()
  457.         amplitude = 0.1
  458.         frequency = 1
  459.         setAngles = true
  460.     end
  461.  
  462.     if (setAngles) then
  463.         local desiredAngle = amplitude * math.sin(time * frequency)
  464.  
  465.         RightShoulder:SetDesiredAngle(desiredAngle + climbFudge)
  466.         LeftShoulder:SetDesiredAngle(desiredAngle - climbFudge)
  467.         RightHip:SetDesiredAngle(-desiredAngle)
  468.         LeftHip:SetDesiredAngle(-desiredAngle)
  469.     end
  470.  
  471.     -- Tool Animation handling
  472.     local tool = getTool()
  473.     if tool and tool:FindFirstChild("Handle") then
  474.    
  475.         local animStringValueObject = getToolAnim(tool)
  476.  
  477.         if animStringValueObject then
  478.             toolAnim = animStringValueObject.Value
  479.             -- message recieved, delete StringValue
  480.             animStringValueObject.Parent = nil
  481.             toolAnimTime = time + .3
  482.         end
  483.  
  484.         if time > toolAnimTime then
  485.             toolAnimTime = 0
  486.             toolAnim = "None"
  487.         end
  488.  
  489.         animateTool()      
  490.     else
  491.         stopToolAnimations()
  492.         toolAnim = "None"
  493.         toolAnimInstance = nil
  494.         toolAnimTime = 0
  495.     end
  496. end
  497.  
  498. -- connect events
  499. Humanoid.Died:connect(onDied)
  500. Humanoid.Running:connect(onRunning)
  501. Humanoid.Jumping:connect(onJumping)
  502. Humanoid.Climbing:connect(onClimbing)
  503. Humanoid.GettingUp:connect(onGettingUp)
  504. Humanoid.FreeFalling:connect(onFreeFall)
  505. Humanoid.FallingDown:connect(onFallingDown)
  506. Humanoid.Seated:connect(onSeated)
  507. Humanoid.PlatformStanding:connect(onPlatformStanding)
  508. Humanoid.Swimming:connect(onSwimming)
  509.  
  510. -- setup emote chat hook
  511. game:GetService("Players").LocalPlayer.Chatted:connect(function(msg)
  512.     local emote = ""
  513.     if msg == "/e dance" then
  514.         emote = dances[math.random(1, #dances)]
  515.     elseif (string.sub(msg, 1, 3) == "/e ") then
  516.         emote = string.sub(msg, 4)
  517.     elseif (string.sub(msg, 1, 7) == "/emote ") then
  518.         emote = string.sub(msg, 8)
  519.     end
  520.    
  521.     if (pose == "Standing" and emoteNames[emote] ~= nil) then
  522.         playAnimation(emote, 0.1, Humanoid)
  523.     end
  524.  
  525. end)
  526.  
  527.  
  528. -- main program
  529.  
  530. -- initialize to idle
  531. playAnimation("idle", 0.1, Humanoid)
  532. pose = "Standing"
  533.  
  534. while Figure.Parent ~= nil do
  535.     local _, time = wait(0.1)
  536.     move(time)
  537. end
  538.  
  539.  
  540.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement