Advertisement
ElykZion

Animation Script

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