Mryeetmemes

Drooling Zombie Animate Script

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