Advertisement
Code_Duckie

Animation Script Test

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