M3dz

Untitled

Jan 4th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.89 KB | Source Code | 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 oldAnimTrack = nil
  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. laugh = {
  67. { id = "http://www.roblox.com/asset/?id=129423131", weight = 10 }
  68. },
  69. cheer = {
  70. { id = "http://www.roblox.com/asset/?id=129423030", weight = 10 }
  71. },
  72. }
  73.  
  74. -- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
  75. local emoteNames = { wave = false, point = false, dance = true, laugh = false, cheer = false}
  76.  
  77. math.randomseed(tick())
  78.  
  79. -- Setup animation objects
  80. for name, fileList in pairs(animNames) do
  81. animTable[name] = {}
  82. animTable[name].count = 0
  83. animTable[name].totalWeight = 0
  84.  
  85. -- check for config values
  86. local config = script:FindFirstChild(name)
  87. if (config ~= nil) then
  88. -- print("Loading anims " .. name)
  89. local idx = 1
  90. for _, childPart in pairs(config:GetChildren()) do
  91. animTable[name][idx] = {}
  92. animTable[name][idx].anim = childPart
  93. local weightObject = childPart:FindFirstChild("Weight")
  94. if (weightObject == nil) then
  95. animTable[name][idx].weight = 1
  96. else
  97. animTable[name][idx].weight = weightObject.Value
  98. end
  99. animTable[name].count = animTable[name].count + 1
  100. animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  101. -- print(name .. " [" .. idx .. "] " .. animTable[name][idx].anim.AnimationId .. " (" .. animTable[name][idx].weight .. ")")
  102. idx = idx + 1
  103. end
  104. end
  105.  
  106. -- fallback to defaults
  107. if (animTable[name].count <= 0) then
  108. for idx, anim in pairs(fileList) do
  109. animTable[name][idx] = {}
  110. animTable[name][idx].anim = Instance.new("Animation")
  111. animTable[name][idx].anim.Name = name
  112. animTable[name][idx].anim.AnimationId = anim.id
  113. animTable[name][idx].weight = anim.weight
  114. animTable[name].count = animTable[name].count + 1
  115. animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  116. -- print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
  117. end
  118. end
  119. end
  120.  
  121. -- ANIMATION
  122.  
  123. -- declarations
  124. local toolAnim = "None"
  125. local toolAnimTime = 0
  126.  
  127. local jumpAnimTime = 0
  128. local jumpAnimDuration = 0.175
  129.  
  130. local toolTransitionTime = 0.1
  131. local fallTransitionTime = 0.2
  132. local jumpMaxLimbVelocity = 0.75
  133.  
  134. -- functions
  135.  
  136. function stopAllAnimations()
  137. local oldAnim = currentAnim
  138.  
  139. -- return to idle if finishing an emote
  140. if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  141. oldAnim = "idle"
  142. end
  143.  
  144. currentAnim = ""
  145. if (currentAnimKeyframeHandler ~= nil) then
  146. currentAnimKeyframeHandler:disconnect()
  147. end
  148.  
  149. if (oldAnimTrack ~= nil) then
  150. oldAnimTrack:Stop()
  151. oldAnimTrack:Destroy()
  152. oldAnimTrack = nil
  153. end
  154. if (currentAnimTrack ~= nil) then
  155. currentAnimTrack:Stop()
  156. currentAnimTrack:Destroy()
  157. currentAnimTrack = nil
  158. end
  159. return oldAnim
  160. end
  161.  
  162. function keyFrameReachedFunc(frameName)
  163. if (frameName == "End") then
  164. -- print("Keyframe : ".. frameName)
  165. local repeatAnim = stopAllAnimations()
  166. playAnimation(repeatAnim, 0.0, Humanoid)
  167. end
  168. end
  169.  
  170. -- Preload animations
  171. function playAnimation(animName, transitionTime, humanoid)
  172. if (animName ~= currentAnim) then
  173.  
  174. if (oldAnimTrack ~= nil) then
  175. oldAnimTrack:Stop()
  176. oldAnimTrack:Destroy()
  177. end
  178.  
  179. local roll = math.random(1, animTable[animName].totalWeight)
  180. local origRoll = roll
  181. local idx = 1
  182. while (roll > animTable[animName][idx].weight) do
  183. roll = roll - animTable[animName][idx].weight
  184. idx = idx + 1
  185. end
  186. -- print(animName .. " " .. idx .. " [" .. origRoll .. "]")
  187. local anim = animTable[animName][idx].anim
  188.  
  189. -- load it to the humanoid; get AnimationTrack
  190. oldAnimTrack = currentAnimTrack
  191. currentAnimTrack = humanoid:LoadAnimation(anim)
  192.  
  193. -- play the animation
  194. currentAnimTrack:Play(transitionTime)
  195. currentAnim = animName
  196.  
  197. -- set up keyframe name triggers
  198. if (currentAnimKeyframeHandler ~= nil) then
  199. currentAnimKeyframeHandler:disconnect()
  200. end
  201. currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  202. end
  203. end
  204.  
  205. -------------------------------------------------------------------------------------------
  206. -------------------------------------------------------------------------------------------
  207.  
  208. local toolAnimName = ""
  209. local toolOldAnimTrack = nil
  210. local toolAnimTrack = nil
  211. local currentToolAnimKeyframeHandler = nil
  212.  
  213. function toolKeyFrameReachedFunc(frameName)
  214. if (frameName == "End") then
  215. -- print("Keyframe : ".. frameName)
  216. local repeatAnim = stopToolAnimations()
  217. playToolAnimation(repeatAnim, 0.0, Humanoid)
  218. end
  219. end
  220.  
  221.  
  222. function playToolAnimation(animName, transitionTime, humanoid)
  223. if (animName ~= toolAnimName) then
  224.  
  225. if (toolAnimTrack ~= nil) then
  226. toolAnimTrack:Stop()
  227. toolAnimTrack:Destroy()
  228. transitionTime = 0
  229. end
  230.  
  231. local roll = math.random(1, animTable[animName].totalWeight)
  232. local origRoll = roll
  233. local idx = 1
  234. while (roll > animTable[animName][idx].weight) do
  235. roll = roll - animTable[animName][idx].weight
  236. idx = idx + 1
  237. end
  238. -- print(animName .. " * " .. idx .. " [" .. origRoll .. "]")
  239. local anim = animTable[animName][idx].anim
  240.  
  241. -- load it to the humanoid; get AnimationTrack
  242. toolOldAnimTrack = toolAnimTrack
  243. toolAnimTrack = humanoid:LoadAnimation(anim)
  244.  
  245. -- play the animation
  246. toolAnimTrack:Play(transitionTime)
  247. toolAnimName = animName
  248.  
  249. currentToolAnimKeyframeHandler = toolAnimTrack.KeyframeReached:connect(toolKeyFrameReachedFunc)
  250. end
  251. end
  252.  
  253. function stopToolAnimations()
  254. local oldAnim = toolAnimName
  255.  
  256. if (currentToolAnimKeyframeHandler ~= nil) then
  257. currentToolAnimKeyframeHandler:disconnect()
  258. end
  259.  
  260. toolAnimName = ""
  261. if (toolAnimTrack ~= nil) then
  262. toolAnimTrack:Stop()
  263. toolAnimTrack:Destroy()
  264. toolAnimTrack = nil
  265. end
  266.  
  267.  
  268. return oldAnim
  269. end
  270.  
  271. -------------------------------------------------------------------------------------------
  272. -------------------------------------------------------------------------------------------
  273.  
  274.  
  275. function onRunning(speed)
  276. if speed>0 then
  277. playAnimation("walk", 0.1, Humanoid)
  278. pose = "Running"
  279. else
  280. playAnimation("idle", 0.1, Humanoid)
  281. pose = "Standing"
  282. end
  283. end
  284.  
  285. function onDied()
  286. pose = "Dead"
  287. end
  288.  
  289. function onJumping()
  290. playAnimation("jump", 0.1, Humanoid)
  291. jumpAnimTime = jumpAnimDuration
  292. pose = "Jumping"
  293. end
  294.  
  295. function onClimbing()
  296. playAnimation("climb", 0.1, Humanoid)
  297. pose = "Climbing"
  298. end
  299.  
  300. function onGettingUp()
  301. pose = "GettingUp"
  302. end
  303.  
  304. function onFreeFall()
  305. if (jumpAnimTime <= 0) then
  306. playAnimation("fall", fallTransitionTime, Humanoid)
  307. end
  308. pose = "FreeFall"
  309. end
  310.  
  311. function onFallingDown()
  312. pose = "FallingDown"
  313. end
  314.  
  315. function onSeated()
  316. pose = "Seated"
  317. end
  318.  
  319. function onPlatformStanding()
  320. pose = "PlatformStanding"
  321. end
  322.  
  323. function onSwimming(speed)
  324. if speed>0 then
  325. pose = "Running"
  326. else
  327. pose = "Standing"
  328. end
  329. end
  330.  
  331. function getTool()
  332. for _, kid in ipairs(Figure:GetChildren()) do
  333. if kid.className == "Tool" then return kid end
  334. end
  335. return nil
  336. end
  337.  
  338. function getToolAnim(tool)
  339. for _, c in ipairs(tool:GetChildren()) do
  340. if c.Name == "toolanim" and c.className == "StringValue" then
  341. return c
  342. end
  343. end
  344. return nil
  345. end
  346.  
  347. function animateTool()
  348.  
  349. if (toolAnim == "None") then
  350. playToolAnimation("toolnone", toolTransitionTime, Humanoid)
  351. return
  352. end
  353.  
  354. if (toolAnim == "Slash") then
  355. playToolAnimation("toolslash", 0, Humanoid)
  356. return
  357. end
  358.  
  359. if (toolAnim == "Lunge") then
  360. playToolAnimation("toollunge", 0, Humanoid)
  361. return
  362. end
  363. end
  364.  
  365. function moveSit()
  366. RightShoulder.MaxVelocity = 0.15
  367. LeftShoulder.MaxVelocity = 0.15
  368. RightShoulder:SetDesiredAngle(3.14 /2)
  369. LeftShoulder:SetDesiredAngle(-3.14 /2)
  370. RightHip:SetDesiredAngle(3.14 /2)
  371. LeftHip:SetDesiredAngle(-3.14 /2)
  372. end
  373.  
  374. local lastTick = 0
  375.  
  376. function move(time)
  377. local amplitude = 1
  378. local frequency = 1
  379. local deltaTime = time - lastTick
  380. lastTick = time
  381.  
  382. local climbFudge = 0
  383. local setAngles = false
  384.  
  385. if (jumpAnimTime > 0) then
  386. jumpAnimTime = jumpAnimTime - deltaTime
  387. end
  388.  
  389. if (pose == "FreeFall" and jumpAnimTime <= 0) then
  390. playAnimation("fall", fallTransitionTime, Humanoid)
  391. elseif (pose == "Seated") then
  392. stopAllAnimations()
  393. moveSit()
  394. return
  395. elseif (pose == "Running") then
  396. playAnimation("walk", 0.1, Humanoid)
  397. elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  398. -- print("Wha " .. pose)
  399. amplitude = 0.1
  400. frequency = 1
  401. setAngles = true
  402. end
  403.  
  404. if (setAngles) then
  405. desiredAngle = amplitude * math.sin(time * frequency)
  406.  
  407. RightShoulder:SetDesiredAngle(desiredAngle + climbFudge)
  408. LeftShoulder:SetDesiredAngle(desiredAngle - climbFudge)
  409. RightHip:SetDesiredAngle(-desiredAngle)
  410. LeftHip:SetDesiredAngle(-desiredAngle)
  411. end
  412.  
  413. -- Tool Animation handling
  414. local tool = getTool()
  415. if tool then
  416.  
  417. animStringValueObject = getToolAnim(tool)
  418.  
  419. if animStringValueObject then
  420. toolAnim = animStringValueObject.Value
  421. -- message recieved, delete StringValue
  422. animStringValueObject.Parent = nil
  423. toolAnimTime = time + .3
  424. end
  425.  
  426. if time > toolAnimTime then
  427. toolAnimTime = 0
  428. toolAnim = "None"
  429. end
  430.  
  431. animateTool()
  432. else
  433. stopToolAnimations()
  434. toolAnim = "None"
  435. toolAnimTime = 0
  436. end
  437. end
  438.  
  439. -- connect events
  440. Humanoid.Died:connect(onDied)
  441. Humanoid.Running:connect(onRunning)
  442. Humanoid.Jumping:connect(onJumping)
  443. Humanoid.Climbing:connect(onClimbing)
  444. Humanoid.GettingUp:connect(onGettingUp)
  445. Humanoid.FreeFalling:connect(onFreeFall)
  446. Humanoid.FallingDown:connect(onFallingDown)
  447. Humanoid.Seated:connect(onSeated)
  448. Humanoid.PlatformStanding:connect(onPlatformStanding)
  449. Humanoid.Swimming:connect(onSwimming)
  450.  
  451.  
  452. -- main program
  453.  
  454. local runService = game:service("RunService");
  455.  
  456. -- initialize to idle
  457. playAnimation("idle", 0.1, Humanoid)
  458. pose = "Standing"
  459.  
  460. while Figure.Parent~=nil do
  461. local _, time = wait(0.1)
  462. move(time)
  463. end
  464.  
  465.  
  466.  
Add Comment
Please, Sign In to add comment