Advertisement
HowAreYu

Default anims

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