1MinuteRoblox

CREATE AN ENEMY NPC WITH ANIMATION Script by 1MinuteRobloxTutorials

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