Advertisement
DanielSiqueira

Custom Animate

Jan 25th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.07 KB | None | 0 0
  1. local plr = game.Players.LocalPlayer
  2. local char = plr.Character or plr.CharacterAdded:Wait()
  3. local hum = char:WaitForChild('Humanoid')
  4.  
  5. local lastSpeed = hum.WalkSpeed
  6. local humChangesPermission = false
  7. local last_input
  8. local click_timestamp = tick()
  9. local running = false
  10. local runInc = 5
  11.  
  12. local effectsTable = {}
  13.  
  14. local repStorage = game:GetService('ReplicatedStorage')
  15. local utilitiesFolder = repStorage:WaitForChild('Utilities')
  16. local inputService = game:GetService('UserInputService')
  17. local cameraUtilities = require(utilitiesFolder:WaitForChild('Camera_Utilities'))
  18. local pose = "Standing"
  19.  
  20. local stringSub = string.sub
  21.  
  22. local userNoUpdateOnLoopSuccess, userNoUpdateOnLoopValue = pcall(function() return UserSettings():IsUserFeatureEnabled("UserNoUpdateOnLoop") end)
  23. local userNoUpdateOnLoop = userNoUpdateOnLoopSuccess and userNoUpdateOnLoopValue
  24. local userAnimationSpeedDampeningSuccess, userAnimationSpeedDampeningValue = pcall(function() return UserSettings():IsUserFeatureEnabled("UserAnimationSpeedDampening") end)
  25. local userAnimationSpeedDampening = userAnimationSpeedDampeningSuccess and userAnimationSpeedDampeningValue
  26.  
  27. local animateScriptEmoteHookFlagExists, animateScriptEmoteHookFlagEnabled = pcall(function()
  28. return UserSettings():IsUserFeatureEnabled("UserAnimateScriptEmoteHook")
  29. end)
  30. local FFlagAnimateScriptEmoteHook = animateScriptEmoteHookFlagExists and animateScriptEmoteHookFlagEnabled
  31.  
  32. local AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
  33. local HumanoidHipHeight = 2
  34.  
  35. local EMOTE_TRANSITION_TIME = 0.1
  36.  
  37. local currentAnim = ""
  38. local currentAnimInstance = nil
  39. local currentAnimTrack = nil
  40. local currentAnimKeyframeHandler = nil
  41. local currentAnimSpeed = 1.0
  42.  
  43. local runAnimTrack = nil
  44. local runAnimKeyframeHandler = nil
  45.  
  46. local animTable = {}
  47. local animNames = {
  48. idle = {
  49. { id = "http://www.roblox.com/asset/?id=507766666", weight = 1 },
  50. { id = "http://www.roblox.com/asset/?id=507766951", weight = 1 },
  51. { id = "http://www.roblox.com/asset/?id=507766388", weight = 9 }
  52. },
  53. walk = {
  54. { id = "rbxassetid://4561782904", weight = 10 }
  55. },
  56. run = {
  57. { id = "rbxassetid://4384282160", weight = 10 }
  58. },
  59. swim = {
  60. { id = "http://www.roblox.com/asset/?id=507784897", weight = 10 }
  61. },
  62. swimidle = {
  63. { id = "http://www.roblox.com/asset/?id=507785072", weight = 10 }
  64. },
  65. jump = {
  66. { id = "http://www.roblox.com/asset/?id=507765000", weight = 10 }
  67. },
  68. fall = {
  69. { id = "http://www.roblox.com/asset/?id=507767968", weight = 10 }
  70. },
  71. climb = {
  72. { id = "http://www.roblox.com/asset/?id=507765644", weight = 10 }
  73. },
  74. sit = {
  75. { id = "http://www.roblox.com/asset/?id=2506281703", weight = 10 }
  76. },
  77. toolnone = {
  78. { id = "http://www.roblox.com/asset/?id=507768375", weight = 10 }
  79. },
  80. toolslash = {
  81. { id = "http://www.roblox.com/asset/?id=522635514", weight = 10 }
  82. },
  83. toollunge = {
  84. { id = "http://www.roblox.com/asset/?id=522638767", weight = 10 }
  85. },
  86. wave = {
  87. { id = "http://www.roblox.com/asset/?id=507770239", weight = 10 }
  88. },
  89. point = {
  90. { id = "http://www.roblox.com/asset/?id=507770453", weight = 10 }
  91. },
  92. dance = {
  93. { id = "http://www.roblox.com/asset/?id=507771019", weight = 10 },
  94. { id = "http://www.roblox.com/asset/?id=507771955", weight = 10 },
  95. { id = "http://www.roblox.com/asset/?id=507772104", weight = 10 }
  96. },
  97. dance2 = {
  98. { id = "http://www.roblox.com/asset/?id=507776043", weight = 10 },
  99. { id = "http://www.roblox.com/asset/?id=507776720", weight = 10 },
  100. { id = "http://www.roblox.com/asset/?id=507776879", weight = 10 }
  101. },
  102. dance3 = {
  103. { id = "http://www.roblox.com/asset/?id=507777268", weight = 10 },
  104. { id = "http://www.roblox.com/asset/?id=507777451", weight = 10 },
  105. { id = "http://www.roblox.com/asset/?id=507777623", weight = 10 }
  106. },
  107. laugh = {
  108. { id = "http://www.roblox.com/asset/?id=507770818", weight = 10 }
  109. },
  110. cheer = {
  111. { id = "http://www.roblox.com/asset/?id=507770677", weight = 10 }
  112. },
  113. }
  114.  
  115. -- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
  116. local emoteNames = { wave = false, point = false, dance = true, dance2 = true, dance3 = true, laugh = false, cheer = false}
  117.  
  118. local PreloadAnimsUserFlag = false
  119. local PreloadedAnims = {}
  120. local successPreloadAnim, msgPreloadAnim = pcall(function()
  121. PreloadAnimsUserFlag = UserSettings():IsUserFeatureEnabled("UserPreloadAnimations")
  122. end)
  123. if not successPreloadAnim then
  124. PreloadAnimsUserFlag = false
  125. end
  126.  
  127. math.randomseed(tick())
  128.  
  129. function findExistingAnimationInSet(set, anim)
  130. if set == nil or anim == nil then
  131. return 0
  132. end
  133.  
  134. for idx = 1, set.count, 1 do
  135. if set[idx].anim.AnimationId == anim.AnimationId then
  136. return idx
  137. end
  138. end
  139.  
  140. return 0
  141. end
  142.  
  143. function configureAnimationSet(name, fileList)
  144. if (animTable[name] ~= nil) then
  145. for _, connection in pairs(animTable[name].connections) do
  146. connection:disconnect()
  147. end
  148. end
  149. animTable[name] = {}
  150. animTable[name].count = 0
  151. animTable[name].totalWeight = 0
  152. animTable[name].connections = {}
  153.  
  154. local allowCustomAnimations = false
  155.  
  156. -- check for config values
  157. local config = script:FindFirstChild(name)
  158. if (allowCustomAnimations and config ~= nil) then
  159. table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  160. table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  161.  
  162. local idx = 0
  163. for _, childPart in pairs(config:GetChildren()) do
  164. if (childPart:IsA("Animation")) then
  165. local newWeight = 1
  166. local weightObject = childPart:FindFirstChild("Weight")
  167. if (weightObject ~= nil) then
  168. newWeight = weightObject.Value
  169. end
  170. animTable[name].count = animTable[name].count + 1
  171. idx = animTable[name].count
  172. animTable[name][idx] = {}
  173. animTable[name][idx].anim = childPart
  174. animTable[name][idx].weight = newWeight
  175. animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  176. table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  177. table.insert(animTable[name].connections, childPart.ChildAdded:connect(function(property) configureAnimationSet(name, fileList) end))
  178. table.insert(animTable[name].connections, childPart.ChildRemoved:connect(function(property) configureAnimationSet(name, fileList) end))
  179. end
  180. end
  181. end
  182.  
  183. -- fallback to defaults
  184. if (animTable[name].count <= 0) then
  185. for idx, anim in pairs(fileList) do
  186. animTable[name][idx] = {}
  187. animTable[name][idx].anim = Instance.new("Animation")
  188. animTable[name][idx].anim.Name = name
  189. animTable[name][idx].anim.AnimationId = anim.id
  190. animTable[name][idx].weight = anim.weight
  191. animTable[name].count = animTable[name].count + 1
  192. animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  193. end
  194. end
  195.  
  196. -- preload anims
  197. if PreloadAnimsUserFlag then
  198. for i, animType in pairs(animTable) do
  199. for idx = 1, animType.count, 1 do
  200. if PreloadedAnims[animType[idx].anim.AnimationId] == nil then
  201. hum:LoadAnimation(animType[idx].anim)
  202. PreloadedAnims[animType[idx].anim.AnimationId] = true
  203. end
  204. end
  205. end
  206. end
  207. end
  208.  
  209. ------------------------------------------------------------------------------------------------------------
  210.  
  211. function configureAnimationSetOld(name, fileList)
  212. if (animTable[name] ~= nil) then
  213. for _, connection in pairs(animTable[name].connections) do
  214. connection:disconnect()
  215. end
  216. end
  217. animTable[name] = {}
  218. animTable[name].count = 0
  219. animTable[name].totalWeight = 0
  220. animTable[name].connections = {}
  221.  
  222. local allowCustomAnimations = true
  223.  
  224. local success, msg = pcall(function() allowCustomAnimations = game:GetService("StarterPlayer").AllowCustomAnimations end)
  225. if not success then
  226. allowCustomAnimations = true
  227. end
  228.  
  229. -- check for config values
  230. local config = script:FindFirstChild(name)
  231. if (allowCustomAnimations and config ~= nil) then
  232. table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  233. table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  234. local idx = 1
  235. for _, childPart in pairs(config:GetChildren()) do
  236. if (childPart:IsA("Animation")) then
  237. table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  238. animTable[name][idx] = {}
  239. animTable[name][idx].anim = childPart
  240. local weightObject = childPart:FindFirstChild("Weight")
  241. if (weightObject == nil) then
  242. animTable[name][idx].weight = 1
  243. else
  244. animTable[name][idx].weight = weightObject.Value
  245. end
  246. animTable[name].count = animTable[name].count + 1
  247. animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  248. idx = idx + 1
  249. end
  250. end
  251. end
  252.  
  253. -- fallback to defaults
  254. if (animTable[name].count <= 0) then
  255. for idx, anim in pairs(fileList) do
  256. animTable[name][idx] = {}
  257. animTable[name][idx].anim = Instance.new("Animation")
  258. animTable[name][idx].anim.Name = name
  259. animTable[name][idx].anim.AnimationId = anim.id
  260. animTable[name][idx].weight = anim.weight
  261. animTable[name].count = animTable[name].count + 1
  262. animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  263. -- print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
  264. end
  265. end
  266.  
  267. -- preload anims
  268. if PreloadAnimsUserFlag then
  269. for i, animType in pairs(animTable) do
  270. for idx = 1, animType.count, 1 do
  271. hum:LoadAnimation(animType[idx].anim)
  272. end
  273. end
  274. end
  275. end
  276.  
  277. -- Setup animation objects
  278. function scriptChildModified(child)
  279. local fileList = animNames[child.Name]
  280. if (fileList ~= nil) then
  281. configureAnimationSet(child.Name, fileList)
  282. end
  283. end
  284.  
  285. script.ChildAdded:connect(scriptChildModified)
  286. script.ChildRemoved:connect(scriptChildModified)
  287.  
  288.  
  289. for name, fileList in pairs(animNames) do
  290. configureAnimationSet(name, fileList)
  291. end
  292.  
  293. -- ANIMATION
  294.  
  295. -- declarations
  296. local toolAnim = "None"
  297. local toolAnimTime = 0
  298.  
  299. local jumpAnimTime = 0
  300. local jumpAnimDuration = 0.31
  301.  
  302. local toolTransitionTime = 0.1
  303. local fallTransitionTime = 0.2
  304.  
  305. local currentlyPlayingEmote = false
  306.  
  307. -- functions
  308.  
  309. function stopAllAnimations()
  310. local oldAnim = currentAnim
  311.  
  312. -- return to idle if finishing an emote
  313. if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  314. oldAnim = "idle"
  315. end
  316.  
  317. if FFlagAnimateScriptEmoteHook and currentlyPlayingEmote then
  318. oldAnim = "idle"
  319. currentlyPlayingEmote = false
  320. end
  321.  
  322. currentAnim = ""
  323. currentAnimInstance = nil
  324. if (currentAnimKeyframeHandler ~= nil) then
  325. currentAnimKeyframeHandler:disconnect()
  326. end
  327.  
  328. if (currentAnimTrack ~= nil) then
  329. currentAnimTrack:Stop()
  330. currentAnimTrack:Destroy()
  331. currentAnimTrack = nil
  332. end
  333.  
  334. -- clean up walk if there is one
  335. if (runAnimKeyframeHandler ~= nil) then
  336. runAnimKeyframeHandler:disconnect()
  337. end
  338.  
  339. if (runAnimTrack ~= nil) then
  340. runAnimTrack:Stop()
  341. runAnimTrack:Destroy()
  342. runAnimTrack = nil
  343. end
  344.  
  345. return oldAnim
  346. end
  347.  
  348. function getHeightScale()
  349. if hum then
  350. if not hum.AutomaticScalingEnabled then
  351. return 1
  352. end
  353.  
  354. local scale = hum.HipHeight / HumanoidHipHeight
  355. if userAnimationSpeedDampening then
  356. if AnimationSpeedDampeningObject == nil then
  357. AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
  358. end
  359. if AnimationSpeedDampeningObject ~= nil then
  360. scale = 1 + (hum.HipHeight - HumanoidHipHeight) * AnimationSpeedDampeningObject.Value / HumanoidHipHeight
  361. end
  362. end
  363. return scale
  364. end
  365. return 1
  366. end
  367.  
  368. local smallButNotZero = 0.0001
  369. function setRunSpeed(speed)
  370. local speedScaled = speed * 1.25
  371. local heightScale = getHeightScale()
  372. local runSpeed = speedScaled / heightScale
  373.  
  374. if runSpeed ~= currentAnimSpeed then
  375. if runSpeed < 0.33 then
  376. currentAnimTrack:AdjustWeight(1.0)
  377. runAnimTrack:AdjustWeight(smallButNotZero)
  378. elseif runSpeed < 0.66 then
  379. local weight = ((runSpeed - 0.33) / 0.33)
  380. currentAnimTrack:AdjustWeight(1.0 - weight + smallButNotZero)
  381. runAnimTrack:AdjustWeight(weight + smallButNotZero)
  382. else
  383. currentAnimTrack:AdjustWeight(smallButNotZero)
  384. runAnimTrack:AdjustWeight(1.0)
  385. end
  386. currentAnimSpeed = runSpeed
  387. runAnimTrack:AdjustSpeed(runSpeed)
  388. currentAnimTrack:AdjustSpeed(runSpeed)
  389. end
  390. end
  391.  
  392. function setAnimationSpeed(speed)
  393. if currentAnim == "walk" then
  394. setRunSpeed(speed)
  395. else
  396. if speed ~= currentAnimSpeed then
  397. currentAnimSpeed = speed
  398. currentAnimTrack:AdjustSpeed(currentAnimSpeed)
  399. end
  400. end
  401. end
  402.  
  403. function keyFrameReachedFunc(frameName)
  404. if (frameName == "End") then
  405. if currentAnim == "walk" then
  406. if userNoUpdateOnLoop == true then
  407. if runAnimTrack.Looped ~= true then
  408. runAnimTrack.TimePosition = 0.0
  409. end
  410. if currentAnimTrack.Looped ~= true then
  411. currentAnimTrack.TimePosition = 0.0
  412. end
  413. else
  414. runAnimTrack.TimePosition = 0.0
  415. currentAnimTrack.TimePosition = 0.0
  416. end
  417. else
  418. local repeatAnim = currentAnim
  419. -- return to idle if finishing an emote
  420. if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
  421. repeatAnim = "idle"
  422. end
  423.  
  424. if FFlagAnimateScriptEmoteHook and currentlyPlayingEmote then
  425. if currentAnimTrack.Looped then
  426. -- Allow the emote to loop
  427. return
  428. end
  429.  
  430. repeatAnim = "idle"
  431. currentlyPlayingEmote = false
  432. end
  433.  
  434. local animSpeed = currentAnimSpeed
  435. playAnimation(repeatAnim, 0.15, hum)
  436. setAnimationSpeed(animSpeed)
  437. end
  438. end
  439. end
  440.  
  441. function rollAnimation(animName)
  442. local roll = math.random(1, animTable[animName].totalWeight)
  443. local origRoll = roll
  444. local idx = 1
  445. while (roll > animTable[animName][idx].weight) do
  446. roll = roll - animTable[animName][idx].weight
  447. idx = idx + 1
  448. end
  449. return idx
  450. end
  451.  
  452. local function switchToAnim(anim, animName, transitionTime, humanoid)
  453. -- switch animation
  454. if (anim ~= currentAnimInstance) then
  455.  
  456. if (currentAnimTrack ~= nil) then
  457. currentAnimTrack:Stop(transitionTime)
  458. currentAnimTrack:Destroy()
  459. end
  460.  
  461. if (runAnimTrack ~= nil) then
  462. runAnimTrack:Stop(transitionTime)
  463. runAnimTrack:Destroy()
  464. if userNoUpdateOnLoop == true then
  465. runAnimTrack = nil
  466. end
  467. end
  468.  
  469. currentAnimSpeed = 1.0
  470.  
  471. -- load it to the humanoid; get AnimationTrack
  472. currentAnimTrack = humanoid:LoadAnimation(anim)
  473. currentAnimTrack.Priority = Enum.AnimationPriority.Core
  474.  
  475. -- play the animation
  476. currentAnimTrack:Play(transitionTime)
  477. currentAnim = animName
  478. currentAnimInstance = anim
  479.  
  480. -- set up keyframe name triggers
  481. if (currentAnimKeyframeHandler ~= nil) then
  482. currentAnimKeyframeHandler:disconnect()
  483. end
  484. currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  485.  
  486. -- check to see if we need to blend a walk/run animation
  487. if animName == "walk" then
  488. local runAnimName = "walk"
  489. local runIdx = rollAnimation(runAnimName)
  490.  
  491. runAnimTrack = humanoid:LoadAnimation(animTable[runAnimName][runIdx].anim)
  492. runAnimTrack.Priority = Enum.AnimationPriority.Core
  493. runAnimTrack:Play(transitionTime)
  494.  
  495. if (runAnimKeyframeHandler ~= nil) then
  496. runAnimKeyframeHandler:disconnect()
  497. end
  498. runAnimKeyframeHandler = runAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  499. end
  500. end
  501. end
  502.  
  503. function playAnimation(animName, transitionTime, humanoid)
  504. local idx = rollAnimation(animName)
  505. local anim = animTable[animName][idx].anim
  506.  
  507. switchToAnim(anim, animName, transitionTime, humanoid)
  508. currentlyPlayingEmote = false
  509. end
  510.  
  511. function playEmote(emoteAnim, transitionTime, humanoid)
  512. switchToAnim(emoteAnim, emoteAnim.Name, transitionTime, humanoid)
  513. currentlyPlayingEmote = true
  514. end
  515.  
  516. -------------------------------------------------------------------------------------------
  517. -------------------------------------------------------------------------------------------
  518.  
  519. local toolAnimName = ""
  520. local toolAnimTrack = nil
  521. local toolAnimInstance = nil
  522. local currentToolAnimKeyframeHandler = nil
  523.  
  524. function toolKeyFrameReachedFunc(frameName)
  525. if (frameName == "End") then
  526. playToolAnimation(toolAnimName, 0.0, hum)
  527. end
  528. end
  529.  
  530.  
  531. function playToolAnimation(animName, transitionTime, humanoid, priority)
  532. local idx = rollAnimation(animName)
  533. local anim = animTable[animName][idx].anim
  534.  
  535. if (toolAnimInstance ~= anim) then
  536.  
  537. if (toolAnimTrack ~= nil) then
  538. toolAnimTrack:Stop()
  539. toolAnimTrack:Destroy()
  540. transitionTime = 0
  541. end
  542.  
  543. -- load it to the humanoid; get AnimationTrack
  544. toolAnimTrack = humanoid:LoadAnimation(anim)
  545. if priority then
  546. toolAnimTrack.Priority = priority
  547. end
  548.  
  549. -- play the animation
  550. toolAnimTrack:Play(transitionTime)
  551. toolAnimName = animName
  552. toolAnimInstance = anim
  553.  
  554. currentToolAnimKeyframeHandler = toolAnimTrack.KeyframeReached:connect(toolKeyFrameReachedFunc)
  555. end
  556. end
  557.  
  558. function stopToolAnimations()
  559. local oldAnim = toolAnimName
  560.  
  561. if (currentToolAnimKeyframeHandler ~= nil) then
  562. currentToolAnimKeyframeHandler:disconnect()
  563. end
  564.  
  565. toolAnimName = ""
  566. toolAnimInstance = nil
  567. if (toolAnimTrack ~= nil) then
  568. toolAnimTrack:Stop()
  569. toolAnimTrack:Destroy()
  570. toolAnimTrack = nil
  571. end
  572.  
  573. return oldAnim
  574. end
  575.  
  576. -------------------------------------------------------------------------------------------
  577. -------------------------------------------------------------------------------------------
  578. -- STATE CHANGE HANDLERS
  579.  
  580. function onRunning(speed)
  581. if speed > 0.75 then
  582. local scale = 16.0
  583. local animationToPlay = running and "run" or "walk"
  584. playAnimation(animationToPlay, 0.2, hum)
  585. setAnimationSpeed(speed / scale)
  586. pose = running and "Running" or "Walking"
  587. else
  588. if emoteNames[currentAnim] == nil and not currentlyPlayingEmote then
  589. playAnimation("idle", 0.2, hum)
  590. pose = "Standing"
  591. end
  592. end
  593. end
  594.  
  595. function onDied()
  596. pose = "Dead"
  597. end
  598.  
  599. function onJumping()
  600. playAnimation("jump", 0.1, hum)
  601. jumpAnimTime = jumpAnimDuration
  602. pose = "Jumping"
  603. end
  604.  
  605. function onClimbing(speed)
  606. local scale = 5.0
  607. playAnimation("climb", 0.1, hum)
  608. setAnimationSpeed(speed / scale)
  609. pose = "Climbing"
  610. end
  611.  
  612. function onGettingUp()
  613. pose = "GettingUp"
  614. end
  615.  
  616. function onFreeFall()
  617. if (jumpAnimTime <= 0) then
  618. playAnimation("fall", fallTransitionTime, hum)
  619. end
  620. pose = "FreeFall"
  621. end
  622.  
  623. function onFallingDown()
  624. pose = "FallingDown"
  625. end
  626.  
  627. function onSeated()
  628. pose = "Seated"
  629. end
  630.  
  631. function onPlatformStanding()
  632. pose = "PlatformStanding"
  633. end
  634.  
  635. -------------------------------------------------------------------------------------------
  636. -------------------------------------------------------------------------------------------
  637.  
  638. function onSwimming(speed)
  639. if speed > 1.00 then
  640. local scale = 10.0
  641. playAnimation("swim", 0.4, hum)
  642. setAnimationSpeed(speed / scale)
  643. pose = "Swimming"
  644. else
  645. playAnimation("swimidle", 0.4, hum)
  646. pose = "Standing"
  647. end
  648. end
  649.  
  650. function animateTool()
  651. if (toolAnim == "None") then
  652. playToolAnimation("toolnone", toolTransitionTime, hum, Enum.AnimationPriority.Idle)
  653. return
  654. end
  655.  
  656. if (toolAnim == "Slash") then
  657. playToolAnimation("toolslash", 0, hum, Enum.AnimationPriority.Action)
  658. return
  659. end
  660.  
  661. if (toolAnim == "Lunge") then
  662. playToolAnimation("toollunge", 0, hum, Enum.AnimationPriority.Action)
  663. return
  664. end
  665. end
  666.  
  667. function getToolAnim(tool)
  668. for _, c in ipairs(tool:GetChildren()) do
  669. if c.Name == "toolanim" and c.className == "StringValue" then
  670. return c
  671. end
  672. end
  673. return nil
  674. end
  675.  
  676. local lastTick = 0
  677.  
  678. function stepAnimate(currentTime)
  679. local amplitude = 1
  680. local frequency = 1
  681. local deltaTime = currentTime - lastTick
  682. lastTick = currentTime
  683.  
  684. local climbFudge = 0
  685. local setAngles = false
  686.  
  687. if (jumpAnimTime > 0) then
  688. jumpAnimTime = jumpAnimTime - deltaTime
  689. end
  690.  
  691. if (pose == "FreeFall" and jumpAnimTime <= 0) then
  692. playAnimation("fall", fallTransitionTime, hum)
  693. elseif (pose == "Seated") then
  694. playAnimation("sit", 0.5, hum)
  695. return
  696. elseif (pose == "Running") then
  697. playAnimation("run", 0.2, hum)
  698. elseif (pose == "Walking") then
  699. playAnimation("walk", 0.2, hum)
  700. elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  701. stopAllAnimations()
  702. amplitude = 0.1
  703. frequency = 1
  704. setAngles = true
  705. end
  706.  
  707. -- Tool Animation handling
  708. local tool = char:FindFirstChildOfClass("Tool")
  709. if tool and tool:FindFirstChild("Handle") then
  710. local animStringValueObject = getToolAnim(tool)
  711.  
  712. if animStringValueObject then
  713. toolAnim = animStringValueObject.Value
  714. -- message recieved, delete StringValue
  715. animStringValueObject.Parent = nil
  716. toolAnimTime = currentTime + .3
  717. end
  718.  
  719. if currentTime > toolAnimTime then
  720. toolAnimTime = 0
  721. toolAnim = "None"
  722. end
  723.  
  724. animateTool()
  725. else
  726. stopToolAnimations()
  727. toolAnim = "None"
  728. toolAnimInstance = nil
  729. toolAnimTime = 0
  730. end
  731. end
  732.  
  733. -- connect events
  734. hum.Died:connect(onDied)
  735. hum.Running:connect(onRunning)
  736. hum.Jumping:connect(onJumping)
  737. hum.Climbing:connect(onClimbing)
  738. hum.GettingUp:connect(onGettingUp)
  739. hum.FreeFalling:connect(onFreeFall)
  740. hum.FallingDown:connect(onFallingDown)
  741. hum.Seated:connect(onSeated)
  742. hum.PlatformStanding:connect(onPlatformStanding)
  743. hum.Swimming:connect(onSwimming)
  744.  
  745. -- setup emote chat hook
  746. game:GetService("Players").LocalPlayer.Chatted:connect(function(msg)
  747. local emote = ""
  748. if (stringSub(msg, 1, 3) == "/e ") then
  749. emote = stringSub(msg, 4)
  750. elseif (stringSub(msg, 1, 7) == "/emote ") then
  751. emote = stringSub(msg, 8)
  752. end
  753.  
  754. if (pose == "Standing" and emoteNames[emote] ~= nil) then
  755. playAnimation(emote, EMOTE_TRANSITION_TIME, hum)
  756. end
  757. end)
  758.  
  759. -- emote bindable hook
  760. if FFlagAnimateScriptEmoteHook then
  761. script:WaitForChild("PlayEmote").OnInvoke = function(emote)
  762. -- Only play emotes when idling
  763. if pose ~= "Standing" then
  764. return
  765. end
  766.  
  767. if emoteNames[emote] ~= nil then
  768. -- Default emotes
  769. playAnimation(emote, EMOTE_TRANSITION_TIME, hum)
  770.  
  771. return true
  772. elseif typeof(emote) == "Instance" and emote:IsA("Animation") then
  773. -- Non-default emotes
  774. playEmote(emote, EMOTE_TRANSITION_TIME, hum)
  775. return true
  776. end
  777.  
  778. -- Return false to indicate that the emote could not be played
  779. return false
  780. end
  781. end
  782.  
  783. -- initialize to idle
  784. playAnimation("idle", 0.1, hum)
  785. pose = "Standing"
  786.  
  787. -- loop to handle timed state transitions and tool animations
  788.  
  789. inputService.InputBegan:Connect(function(input,processed)
  790. if not processed then
  791. if input.KeyCode == Enum.KeyCode.W and last_input == Enum.KeyCode.W and tick() - click_timestamp <= 0.4 and not running then
  792. print('Starting running...')
  793. running = true
  794. pose = 'Running'
  795. --hum.WalkSpeed = hum.WalkSpeed + runInc
  796. if effectsTable['sprintEffect'] then effectsTable['sprintEffect']:Cancel() end
  797. effectsTable['sprintEffect'] = cameraUtilities:ZoomCamera(1,Enum.EasingDirection.Out,1)
  798.  
  799. humChangesPermission = true
  800. hum.WalkSpeed = hum.WalkSpeed + runInc
  801. humChangesPermission = false
  802. end
  803. last_input = input.KeyCode
  804. end
  805. end)
  806.  
  807. inputService.InputEnded:Connect(function(input,processed)
  808. if not processed then
  809. click_timestamp = tick()
  810. if input.KeyCode == Enum.KeyCode.W and running then
  811. print('Stopping running...')
  812. last_input = nil
  813. running = false
  814. if effectsTable['sprintEffect'] then effectsTable['sprintEffect']:Cancel() end
  815. effectsTable['sprintEffect'] = cameraUtilities:ZoomCamera(1,Enum.EasingDirection.In,1)
  816.  
  817. humChangesPermission = true
  818. hum.WalkSpeed = hum.WalkSpeed - runInc
  819. humChangesPermission = false
  820. end
  821. end
  822. end)
  823.  
  824. hum:GetPropertyChangedSignal('WalkSpeed'):Connect(function()
  825. if not humChangesPermission then
  826. ---hum.WalkSpeed = lastSpeed
  827. end
  828. end)
  829.  
  830. while char.Parent ~= nil do
  831. local _, currentGameTime = wait(0.1)
  832. stepAnimate(currentGameTime)
  833. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement