Toxicator1

UpdatedAnimate

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