Advertisement
Guest User

Script

a guest
Mar 1st, 2021
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.08 KB | None | 0 0
  1. local Character = script.Parent
  2. local Humanoid = Character:WaitForChild("Humanoid")
  3. local pose = "Standing"
  4.  
  5. local currentAnim = ""
  6. local currentAnimInstance = nil
  7. local currentAnimTrack = nil
  8. local currentAnimKeyframeHandler = nil
  9. local currentAnimSpeed = 1.0
  10.  
  11. local runAnimTrack = nil
  12. local runAnimKeyframeHandler = nil
  13.  
  14. local animTable = {}
  15. local animNames = {
  16. idle = {
  17. { id = "http://www.roblox.com/asset/?id=507766666", weight = 1 },
  18. { id = "http://www.roblox.com/asset/?id=507766951", weight = 1 },
  19. { id = "http://www.roblox.com/asset/?id=507766388", weight = 9 }
  20. },
  21. walk = {
  22. { id = "http://www.roblox.com/asset/?id=507777826", weight = 10 }
  23. },
  24. run = {
  25. { id = "http://www.roblox.com/asset/?id=507767714", weight = 10 }
  26. },
  27. swim = {
  28. { id = "http://www.roblox.com/asset/?id=507784897", weight = 10 }
  29. },
  30. swimidle = {
  31. { id = "http://www.roblox.com/asset/?id=507785072", weight = 10 }
  32. },
  33. jump = {
  34. { id = "http://www.roblox.com/asset/?id=507765000", weight = 10 }
  35. },
  36. fall = {
  37. { id = "http://www.roblox.com/asset/?id=507767968", weight = 10 }
  38. },
  39. climb = {
  40. { id = "http://www.roblox.com/asset/?id=507765644", weight = 10 }
  41. },
  42. sit = {
  43. { id = "http://www.roblox.com/asset/?id=507768133", weight = 10 }
  44. },
  45. toolnone = {
  46. { id = "http://www.roblox.com/asset/?id=507768375", weight = 10 }
  47. },
  48. toolslash = {
  49. { id = "http://www.roblox.com/asset/?id=522635514", weight = 10 }
  50. },
  51. toollunge = {
  52. { id = "http://www.roblox.com/asset/?id=522638767", weight = 10 }
  53. },
  54. wave = {
  55. { id = "http://www.roblox.com/asset/?id=507770239", weight = 10 }
  56. },
  57. point = {
  58. { id = "http://www.roblox.com/asset/?id=507770453", weight = 10 }
  59. },
  60. dance = {
  61. { id = "http://www.roblox.com/asset/?id=507771019", weight = 10 },
  62. { id = "http://www.roblox.com/asset/?id=507771955", weight = 10 },
  63. { id = "http://www.roblox.com/asset/?id=507772104", weight = 10 }
  64. },
  65. dance2 = {
  66. { id = "http://www.roblox.com/asset/?id=507776043", weight = 10 },
  67. { id = "http://www.roblox.com/asset/?id=507776720", weight = 10 },
  68. { id = "http://www.roblox.com/asset/?id=507776879", weight = 10 }
  69. },
  70. dance3 = {
  71. { id = "http://www.roblox.com/asset/?id=507777268", weight = 10 },
  72. { id = "http://www.roblox.com/asset/?id=507777451", weight = 10 },
  73. { id = "http://www.roblox.com/asset/?id=507777623", weight = 10 }
  74. },
  75. laugh = {
  76. { id = "http://www.roblox.com/asset/?id=507770818", weight = 10 }
  77. },
  78. cheer = {
  79. { id = "http://www.roblox.com/asset/?id=507770677", weight = 10 }
  80. },
  81. }
  82.  
  83. -- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
  84. local emoteNames = { wave = false, point = false, dance = true, dance2 = true, dance3 = true, laugh = false, cheer = false}
  85.  
  86. math.randomseed(tick())
  87.  
  88. function configureAnimationSet(name, fileList)
  89. if (animTable[name] ~= nil) then
  90. for _, connection in pairs(animTable[name].connections) do
  91. connection:disconnect()
  92. end
  93. end
  94. animTable[name] = {}
  95. animTable[name].count = 0
  96. animTable[name].totalWeight = 0
  97. animTable[name].connections = {}
  98.  
  99. local allowCustomAnimations = true
  100. local AllowDisableCustomAnimsUserFlag = false
  101.  
  102. local success, msg = pcall(function()
  103. AllowDisableCustomAnimsUserFlag = UserSettings():IsUserFeatureEnabled("UserAllowDisableCustomAnims2")
  104. end)
  105.  
  106. if (AllowDisableCustomAnimsUserFlag) then
  107. allowCustomAnimations = game:GetService("StarterPlayer").AllowCustomAnimations
  108. end
  109.  
  110. -- check for config values
  111. local config = script:FindFirstChild(name)
  112. if (allowCustomAnimations and config ~= nil) then
  113. table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  114. table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  115. local idx = 1
  116. for _, childPart in pairs(config:GetChildren()) do
  117. if (childPart:IsA("Animation")) then
  118. table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  119. animTable[name][idx] = {}
  120. animTable[name][idx].anim = childPart
  121. local weightObject = childPart:FindFirstChild("Weight")
  122. if (weightObject == nil) then
  123. animTable[name][idx].weight = 1
  124. else
  125. animTable[name][idx].weight = weightObject.Value
  126. end
  127. animTable[name].count = animTable[name].count + 1
  128. animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  129. idx = idx + 1
  130. end
  131. end
  132. end
  133.  
  134. -- fallback to defaults
  135. if (animTable[name].count <= 0) then
  136. for idx, anim in pairs(fileList) do
  137. animTable[name][idx] = {}
  138. animTable[name][idx].anim = Instance.new("Animation")
  139. animTable[name][idx].anim.Name = name
  140. animTable[name][idx].anim.AnimationId = anim.id
  141. animTable[name][idx].weight = anim.weight
  142. animTable[name].count = animTable[name].count + 1
  143. animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  144. -- print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
  145. end
  146. end
  147. end
  148.  
  149. -- Setup animation objects
  150. function scriptChildModified(child)
  151. local fileList = animNames[child.Name]
  152. if (fileList ~= nil) then
  153. configureAnimationSet(child.Name, fileList)
  154. end
  155. end
  156.  
  157. script.ChildAdded:connect(scriptChildModified)
  158. script.ChildRemoved:connect(scriptChildModified)
  159.  
  160.  
  161. for name, fileList in pairs(animNames) do
  162. configureAnimationSet(name, fileList)
  163. end
  164.  
  165. -- ANIMATION
  166.  
  167. -- declarations
  168. local toolAnim = "None"
  169. local toolAnimTime = 0
  170.  
  171. local jumpAnimTime = 0
  172. local jumpAnimDuration = 0.31
  173.  
  174. local toolTransitionTime = 0.1
  175. local fallTransitionTime = 0.2
  176.  
  177. -- functions
  178.  
  179. function stopAllAnimations()
  180. local oldAnim = currentAnim
  181.  
  182. -- return to idle if finishing an emote
  183. if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  184. oldAnim = "idle"
  185. end
  186.  
  187. currentAnim = ""
  188. currentAnimInstance = nil
  189. if (currentAnimKeyframeHandler ~= nil) then
  190. currentAnimKeyframeHandler:disconnect()
  191. end
  192.  
  193. if (currentAnimTrack ~= nil) then
  194. currentAnimTrack:Stop()
  195. currentAnimTrack:Destroy()
  196. currentAnimTrack = nil
  197. end
  198.  
  199. -- clean up walk if there is one
  200. if (runAnimKeyframeHandler ~= nil) then
  201. runAnimKeyframeHandler:disconnect()
  202. end
  203.  
  204. if (runAnimTrack ~= nil) then
  205. runAnimTrack:Stop()
  206. runAnimTrack:Destroy()
  207. runAnimTrack = nil
  208. end
  209.  
  210. return oldAnim
  211. end
  212.  
  213. function getHeightScale()
  214. if Humanoid then
  215. local bodyHeightScale = Humanoid:FindFirstChild("BodyHeightScale")
  216. if bodyHeightScale and bodyHeightScale:IsA("NumberValue") then
  217. return bodyHeightScale.Value
  218. end
  219. end
  220.  
  221. return 1
  222. end
  223.  
  224. local smallButNotZero = 0.0001
  225. function setRunSpeed(speed)
  226. if speed < 0.33 then
  227. currentAnimTrack:AdjustWeight(1.0)
  228. runAnimTrack:AdjustWeight(smallButNotZero)
  229. elseif speed < 0.66 then
  230. local weight = ((speed - 0.33) / 0.33)
  231. currentAnimTrack:AdjustWeight(1.0 - weight + smallButNotZero)
  232. runAnimTrack:AdjustWeight(weight + smallButNotZero)
  233. else
  234. currentAnimTrack:AdjustWeight(smallButNotZero)
  235. runAnimTrack:AdjustWeight(1.0)
  236. end
  237.  
  238. local speedScaled = speed * 1.25
  239.  
  240. local heightScale = getHeightScale()
  241.  
  242. runAnimTrack:AdjustSpeed(speedScaled / heightScale)
  243. currentAnimTrack:AdjustSpeed(speedScaled / heightScale)
  244. end
  245.  
  246.  
  247. function setAnimationSpeed(speed)
  248. if speed ~= currentAnimSpeed then
  249. currentAnimSpeed = speed
  250. if currentAnim == "walk" then
  251. setRunSpeed(speed)
  252. else
  253. currentAnimTrack:AdjustSpeed(currentAnimSpeed)
  254. end
  255. end
  256. end
  257.  
  258. function keyFrameReachedFunc(frameName)
  259. if (frameName == "End") then
  260. if currentAnim == "walk" then
  261. runAnimTrack.TimePosition = 0.0
  262. currentAnimTrack.TimePosition = 0.0
  263. else
  264. local repeatAnim = currentAnim
  265. -- return to idle if finishing an emote
  266. if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
  267. repeatAnim = "idle"
  268. end
  269.  
  270. local animSpeed = currentAnimSpeed
  271. playAnimation(repeatAnim, 0.15, Humanoid)
  272. setAnimationSpeed(animSpeed)
  273. end
  274. end
  275. end
  276.  
  277. function rollAnimation(animName)
  278. local roll = math.random(1, animTable[animName].totalWeight)
  279. local origRoll = roll
  280. local idx = 1
  281. while (roll > animTable[animName][idx].weight) do
  282. roll = roll - animTable[animName][idx].weight
  283. idx = idx + 1
  284. end
  285. return idx
  286. end
  287.  
  288. function playAnimation(animName, transitionTime, humanoid)
  289. local idx = rollAnimation(animName)
  290. local anim = animTable[animName][idx].anim
  291.  
  292. -- switch animation
  293. if (anim ~= currentAnimInstance) then
  294.  
  295. if (currentAnimTrack ~= nil) then
  296. currentAnimTrack:Stop(transitionTime)
  297. currentAnimTrack:Destroy()
  298. end
  299.  
  300. if (runAnimTrack ~= nil) then
  301. runAnimTrack:Stop(transitionTime)
  302. runAnimTrack:Destroy()
  303. end
  304.  
  305. currentAnimSpeed = 1.0
  306.  
  307. -- load it to the humanoid; get AnimationTrack
  308. currentAnimTrack = humanoid:LoadAnimation(anim)
  309. currentAnimTrack.Priority = Enum.AnimationPriority.Core
  310.  
  311. -- play the animation
  312. currentAnimTrack:Play(transitionTime)
  313. currentAnim = animName
  314. currentAnimInstance = anim
  315.  
  316. -- set up keyframe name triggers
  317. if (currentAnimKeyframeHandler ~= nil) then
  318. currentAnimKeyframeHandler:disconnect()
  319. end
  320. currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  321.  
  322. -- check to see if we need to blend a walk/run animation
  323. if animName == "walk" then
  324. local runAnimName = "run"
  325. local runIdx = rollAnimation(runAnimName)
  326.  
  327. runAnimTrack = humanoid:LoadAnimation(animTable[runAnimName][runIdx].anim)
  328. runAnimTrack.Priority = Enum.AnimationPriority.Core
  329. runAnimTrack:Play(transitionTime)
  330.  
  331. if (runAnimKeyframeHandler ~= nil) then
  332. runAnimKeyframeHandler:disconnect()
  333. end
  334. runAnimKeyframeHandler = runAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  335. end
  336. end
  337.  
  338. end
  339.  
  340. -------------------------------------------------------------------------------------------
  341. -------------------------------------------------------------------------------------------
  342.  
  343. local toolAnimName = ""
  344. local toolAnimTrack = nil
  345. local toolAnimInstance = nil
  346. local currentToolAnimKeyframeHandler = nil
  347.  
  348. function toolKeyFrameReachedFunc(frameName)
  349. if (frameName == "End") then
  350. playToolAnimation(toolAnimName, 0.0, Humanoid)
  351. end
  352. end
  353.  
  354.  
  355. function playToolAnimation(animName, transitionTime, humanoid, priority)
  356. local idx = rollAnimation(animName)
  357. local anim = animTable[animName][idx].anim
  358.  
  359. if (toolAnimInstance ~= anim) then
  360.  
  361. if (toolAnimTrack ~= nil) then
  362. toolAnimTrack:Stop()
  363. toolAnimTrack:Destroy()
  364. transitionTime = 0
  365. end
  366.  
  367. -- load it to the humanoid; get AnimationTrack
  368. toolAnimTrack = humanoid:LoadAnimation(anim)
  369. if priority then
  370. toolAnimTrack.Priority = priority
  371. end
  372.  
  373. -- play the animation
  374. toolAnimTrack:Play(transitionTime)
  375. toolAnimName = animName
  376. toolAnimInstance = anim
  377.  
  378. currentToolAnimKeyframeHandler = toolAnimTrack.KeyframeReached:connect(toolKeyFrameReachedFunc)
  379. end
  380. end
  381.  
  382. function stopToolAnimations()
  383. local oldAnim = toolAnimName
  384.  
  385. if (currentToolAnimKeyframeHandler ~= nil) then
  386. currentToolAnimKeyframeHandler:disconnect()
  387. end
  388.  
  389. toolAnimName = ""
  390. toolAnimInstance = nil
  391. if (toolAnimTrack ~= nil) then
  392. toolAnimTrack:Stop()
  393. toolAnimTrack:Destroy()
  394. toolAnimTrack = nil
  395. end
  396.  
  397. return oldAnim
  398. end
  399.  
  400. -------------------------------------------------------------------------------------------
  401. -------------------------------------------------------------------------------------------
  402. -- STATE CHANGE HANDLERS
  403.  
  404. function onRunning(speed)
  405. if speed > 0.5 then
  406. local scale = 16.0
  407. playAnimation("walk", 0.2, Humanoid)
  408. setAnimationSpeed(speed / scale)
  409. pose = "Running"
  410. else
  411. if emoteNames[currentAnim] == nil then
  412. playAnimation("idle", 0.2, Humanoid)
  413. pose = "Standing"
  414. end
  415. end
  416. end
  417.  
  418. function onDied()
  419. pose = "Dead"
  420. end
  421.  
  422. function onJumping()
  423. playAnimation("jump", 0.1, Humanoid)
  424. jumpAnimTime = jumpAnimDuration
  425. pose = "Jumping"
  426. end
  427.  
  428. function onClimbing(speed)
  429. local scale = 5.0
  430. playAnimation("climb", 0.1, Humanoid)
  431. setAnimationSpeed(speed / scale)
  432. pose = "Climbing"
  433. end
  434.  
  435. function onGettingUp()
  436. pose = "GettingUp"
  437. end
  438.  
  439. function onFreeFall()
  440. if (jumpAnimTime <= 0) then
  441. playAnimation("fall", fallTransitionTime, Humanoid)
  442. end
  443. pose = "FreeFall"
  444. end
  445.  
  446. function onFallingDown()
  447. pose = "FallingDown"
  448. end
  449.  
  450. function onSeated()
  451. pose = "Seated"
  452. end
  453.  
  454. function onPlatformStanding()
  455. pose = "PlatformStanding"
  456. end
  457.  
  458. -------------------------------------------------------------------------------------------
  459. -------------------------------------------------------------------------------------------
  460.  
  461. function onSwimming(speed)
  462. if speed > 1.00 then
  463. local scale = 10.0
  464. playAnimation("swim", 0.4, Humanoid)
  465. setAnimationSpeed(speed / scale)
  466. pose = "Swimming"
  467. else
  468. playAnimation("swimidle", 0.4, Humanoid)
  469. pose = "Standing"
  470. end
  471. end
  472.  
  473. function animateTool()
  474. if (toolAnim == "None") then
  475. playToolAnimation("toolnone", toolTransitionTime, Humanoid, Enum.AnimationPriority.Idle)
  476. return
  477. end
  478.  
  479. if (toolAnim == "Slash") then
  480. playToolAnimation("toolslash", 0, Humanoid, Enum.AnimationPriority.Action)
  481. return
  482. end
  483.  
  484. if (toolAnim == "Lunge") then
  485. playToolAnimation("toollunge", 0, Humanoid, Enum.AnimationPriority.Action)
  486. return
  487. end
  488. end
  489.  
  490. function getToolAnim(tool)
  491. for _, c in ipairs(tool:GetChildren()) do
  492. if c.Name == "toolanim" and c.className == "StringValue" then
  493. return c
  494. end
  495. end
  496. return nil
  497. end
  498.  
  499. local lastTick = 0
  500.  
  501. function stepAnimate(currentTime)
  502. local amplitude = 1
  503. local frequency = 1
  504. local deltaTime = currentTime - lastTick
  505. lastTick = currentTime
  506.  
  507. local climbFudge = 0
  508. local setAngles = false
  509.  
  510. if (jumpAnimTime > 0) then
  511. jumpAnimTime = jumpAnimTime - deltaTime
  512. end
  513.  
  514. if (pose == "FreeFall" and jumpAnimTime <= 0) then
  515. playAnimation("fall", fallTransitionTime, Humanoid)
  516. elseif (pose == "Seated") then
  517. playAnimation("sit", 0.5, Humanoid)
  518. return
  519. elseif (pose == "Running") then
  520. playAnimation("walk", 0.2, Humanoid)
  521. elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  522. stopAllAnimations()
  523. amplitude = 0.1
  524. frequency = 1
  525. setAngles = true
  526. end
  527.  
  528. -- Tool Animation handling
  529. local tool = Character:FindFirstChildOfClass("Tool")
  530. if tool and (tool.RequiresHandle or tool:FindFirstChild("Handle")) then
  531.  
  532. local animStringValueObject = getToolAnim(tool)
  533.  
  534. if animStringValueObject then
  535. toolAnim = animStringValueObject.Value
  536. -- message recieved, delete StringValue
  537. animStringValueObject.Parent = nil
  538. toolAnimTime = currentTime + .3
  539. end
  540.  
  541. if currentTime > toolAnimTime then
  542. toolAnimTime = 0
  543. toolAnim = "None"
  544. end
  545.  
  546. animateTool()
  547. else
  548. stopToolAnimations()
  549. toolAnim = "None"
  550. toolAnimInstance = nil
  551. toolAnimTime = 0
  552. end
  553. end
  554.  
  555. -- connect events
  556. Humanoid.Died:connect(onDied)
  557. Humanoid.Running:connect(onRunning)
  558. Humanoid.Jumping:connect(onJumping)
  559. Humanoid.Climbing:connect(onClimbing)
  560. Humanoid.GettingUp:connect(onGettingUp)
  561. Humanoid.FreeFalling:connect(onFreeFall)
  562. Humanoid.FallingDown:connect(onFallingDown)
  563. Humanoid.Seated:connect(onSeated)
  564. Humanoid.PlatformStanding:connect(onPlatformStanding)
  565. Humanoid.Swimming:connect(onSwimming)
  566.  
  567. -- setup emote chat hook
  568. game:GetService("Players").LocalPlayer.Chatted:connect(function(msg)
  569. local emote = ""
  570. if (string.sub(msg, 1, 3) == "/e ") then
  571. emote = string.sub(msg, 4)
  572. elseif (string.sub(msg, 1, 7) == "/emote ") then
  573. emote = string.sub(msg, 8)
  574. end
  575.  
  576. if (pose == "Standing" and emoteNames[emote] ~= nil) then
  577. playAnimation(emote, 0.1, Humanoid)
  578. end
  579. end)
  580.  
  581.  
  582.  
  583. -- initialize to idle
  584. playAnimation("idle", 0.1, Humanoid)
  585. pose = "Standing"
  586.  
  587. -- loop to handle timed state transitions and tool animations
  588. while Character.Parent ~= nil do
  589. local _, currentGameTime = wait(0.1)
  590. stepAnimate(currentGameTime)
  591. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement