SHARE
TWEET

custom animation

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