Guest User

Untitled

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