Advertisement
marioisdabomb

test animation

Nov 29th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if game:GetService("RunService"):IsClient() then error("Script must be server-side in order to work; use h/ and not hl/") end
  2. local Player,Mouse,mouse,UserInputService,ContextActionService = owner
  3. do
  4. print("FE Compatibility code by Mokiros | Translated to FE by iPxter")
  5. script.Parent = Player.Character
  6.  
  7. --RemoteEvent for communicating
  8. local Event = Instance.new("RemoteEvent")
  9. Event.Name = "UserInput_Event"
  10.  
  11. --Fake event to make stuff like Mouse.KeyDown work
  12. local function fakeEvent()
  13. local t = {_fakeEvent=true,Connect=function(self,f)self.Function=f end}
  14. t.connect = t.Connect
  15. return t
  16. end
  17.  
  18. --Creating fake input objects with fake variables
  19. local m = {Target=nil,Hit=CFrame.new(),KeyUp=fakeEvent(),KeyDown=fakeEvent(),Button1Up=fakeEvent(),Button1Down=fakeEvent()}
  20. local UIS = {InputBegan=fakeEvent(),InputEnded=fakeEvent()}
  21. local CAS = {Actions={},BindAction=function(self,name,fun,touch,...)
  22. CAS.Actions[name] = fun and {Name=name,Function=fun,Keys={...}} or nil
  23. end}
  24. --Merged 2 functions into one by checking amount of arguments
  25. CAS.UnbindAction = CAS.BindAction
  26.  
  27. --This function will trigger the events that have been :Connect()'ed
  28. local function te(self,ev,...)
  29. local t = m[ev]
  30. if t and t._fakeEvent and t.Function then
  31. t.Function(...)
  32. end
  33. end
  34. m.TrigEvent = te
  35. UIS.TrigEvent = te
  36.  
  37. Event.OnServerEvent:Connect(function(plr,io)
  38. if plr~=Player then return end
  39. if io.isMouse then
  40. m.Target = io.Target
  41. m.Hit = io.Hit
  42. else
  43. local b = io.UserInputState == Enum.UserInputState.Begin
  44. if io.UserInputType == Enum.UserInputType.MouseButton1 then
  45. return m:TrigEvent(b and "Button1Down" or "Button1Up")
  46. end
  47. for _,t in pairs(CAS.Actions) do
  48. for _,k in pairs(t.Keys) do
  49. if k==io.KeyCode then
  50. t.Function(t.Name,io.UserInputState,io)
  51. end
  52. end
  53. end
  54. m:TrigEvent(b and "KeyDown" or "KeyUp",io.KeyCode.Name:lower())
  55. UIS:TrigEvent(b and "InputBegan" or "InputEnded",io,false)
  56. end
  57. end)
  58. Event.Parent = NLS([==[
  59. local Player = game:GetService("Players").LocalPlayer
  60. local Event = script:WaitForChild("UserInput_Event")
  61.  
  62. local UIS = game:GetService("UserInputService")
  63. local input = function(io,a)
  64. if a then return end
  65. --Since InputObject is a client-side instance, we create and pass table instead
  66. Event:FireServer({KeyCode=io.KeyCode,UserInputType=io.UserInputType,UserInputState=io.UserInputState})
  67. end
  68. UIS.InputBegan:Connect(input)
  69. UIS.InputEnded:Connect(input)
  70.  
  71. local Mouse = Player:GetMouse()
  72. local h,t
  73. --Give the server mouse data 30 times every second, but only if the values changed
  74. --If player is not moving their mouse, client won't fire events
  75. while wait(1/30) do
  76. if h~=Mouse.Hit or t~=Mouse.Target then
  77. h,t=Mouse.Hit,Mouse.Target
  78. Event:FireServer({isMouse=true,Target=t,Hit=h})
  79. end
  80. end]==],Player.Character)
  81. Mouse,mouse,UserInputService,ContextActionService = m,m,UIS,CAS
  82. end
  83. function waitForChild(parent, childName)
  84. local child = parent:findFirstChild(childName)
  85. if child then return child end
  86. while true do
  87. child = parent.ChildAdded:wait()
  88. if child.Name==childName then return child end
  89. end
  90. end
  91.  
  92. local Figure = script.Parent
  93. local Torso = waitForChild(Figure, "Torso")
  94. local RightShoulder = waitForChild(Torso, "Right Shoulder")
  95. local LeftShoulder = waitForChild(Torso, "Left Shoulder")
  96. local RightHip = waitForChild(Torso, "Right Hip")
  97. local LeftHip = waitForChild(Torso, "Left Hip")
  98. local Neck = waitForChild(Torso, "Neck")
  99. local Humanoid = waitForChild(Figure, "Humanoid")
  100. local pose = "Standing"
  101.  
  102. local currentAnim = ""
  103. local currentAnimInstance = nil
  104. local currentAnimTrack = nil
  105. local currentAnimKeyframeHandler = nil
  106. local currentAnimSpeed = 1.0
  107. local animTable = {}
  108. local animNames = {
  109. idle = {
  110. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 9 },
  111. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 1 }
  112. },
  113. walk = {
  114. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  115. },
  116. run = {
  117. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  118. },
  119. jump = {
  120. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  121. },
  122. fall = {
  123. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  124. },
  125. climb = {
  126. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  127. },
  128. sit = {
  129. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  130. },
  131. toolnone = {
  132. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  133. },
  134. toolslash = {
  135. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  136. -- { id = "slash.xml", weight = 10 }
  137. },
  138. toollunge = {
  139. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  140. },
  141. wave = {
  142. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  143. },
  144. point = {
  145. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  146. },
  147. dance1 = {
  148. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 },
  149. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 },
  150. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  151. },
  152. dance2 = {
  153. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 },
  154. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 },
  155. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  156. },
  157. dance3 = {
  158. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 },
  159. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 },
  160. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  161. },
  162. laugh = {
  163. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  164. },
  165. cheer = {
  166. { id = "http://www.roblox.com/asset/?id=1949023689", weight = 10 }
  167. },
  168. }
  169. local dances = {"dance1", "dance2", "dance3"}
  170.  
  171. -- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
  172. local emoteNames = { wave = false, point = false, dance1 = true, dance2 = true, dance3 = true, laugh = false, cheer = false}
  173.  
  174. function configureAnimationSet(name, fileList)
  175. if (animTable[name] ~= nil) then
  176. for _, connection in pairs(animTable[name].connections) do
  177. connection:disconnect()
  178. end
  179. end
  180. animTable[name] = {}
  181. animTable[name].count = 0
  182. animTable[name].totalWeight = 0
  183. animTable[name].connections = {}
  184.  
  185. -- check for config values
  186. local config = script:FindFirstChild(name)
  187. if (config ~= nil) then
  188. -- print("Loading anims " .. name)
  189. table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  190. table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  191. local idx = 1
  192. for _, childPart in pairs(config:GetChildren()) do
  193. if (childPart:IsA("Animation")) then
  194. table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  195. animTable[name][idx] = {}
  196. animTable[name][idx].anim = childPart
  197. local weightObject = childPart:FindFirstChild("Weight")
  198. if (weightObject == nil) then
  199. animTable[name][idx].weight = 1
  200. else
  201. animTable[name][idx].weight = weightObject.Value
  202. end
  203. animTable[name].count = animTable[name].count + 1
  204. animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  205. -- print(name .. " [" .. idx .. "] " .. animTable[name][idx].anim.AnimationId .. " (" .. animTable[name][idx].weight .. ")")
  206. idx = idx + 1
  207. end
  208. end
  209. end
  210.  
  211. -- fallback to defaults
  212. if (animTable[name].count <= 0) then
  213. for idx, anim in pairs(fileList) do
  214. animTable[name][idx] = {}
  215. animTable[name][idx].anim = Instance.new("Animation")
  216. animTable[name][idx].anim.Name = name
  217. animTable[name][idx].anim.AnimationId = anim.id
  218. animTable[name][idx].weight = anim.weight
  219. animTable[name].count = animTable[name].count + 1
  220. animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  221. -- print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
  222. end
  223. end
  224. end
  225.  
  226. -- Setup animation objects
  227. function scriptChildModified(child)
  228. local fileList = animNames[child.Name]
  229. if (fileList ~= nil) then
  230. configureAnimationSet(child.Name, fileList)
  231. end
  232. end
  233.  
  234. script.ChildAdded:connect(scriptChildModified)
  235. script.ChildRemoved:connect(scriptChildModified)
  236.  
  237.  
  238. for name, fileList in pairs(animNames) do
  239. configureAnimationSet(name, fileList)
  240. end
  241.  
  242. -- ANIMATION
  243.  
  244. -- declarations
  245. local toolAnim = "None"
  246. local toolAnimTime = 0
  247.  
  248. local jumpAnimTime = 0
  249. local jumpAnimDuration = 0.3
  250.  
  251. local toolTransitionTime = 0.1
  252. local fallTransitionTime = 0.3
  253. local jumpMaxLimbVelocity = 0.75
  254.  
  255. -- functions
  256.  
  257. function stopAllAnimations()
  258. local oldAnim = currentAnim
  259.  
  260. -- return to idle if finishing an emote
  261. if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  262. oldAnim = "idle"
  263. end
  264.  
  265. currentAnim = ""
  266. currentAnimInstance = nil
  267. if (currentAnimKeyframeHandler ~= nil) then
  268. currentAnimKeyframeHandler:disconnect()
  269. end
  270.  
  271. if (currentAnimTrack ~= nil) then
  272. currentAnimTrack:Stop()
  273. currentAnimTrack:Destroy()
  274. currentAnimTrack = nil
  275. end
  276. return oldAnim
  277. end
  278.  
  279. function setAnimationSpeed(speed)
  280. if speed ~= currentAnimSpeed then
  281. currentAnimSpeed = speed
  282. currentAnimTrack:AdjustSpeed(currentAnimSpeed)
  283. end
  284. end
  285.  
  286. function keyFrameReachedFunc(frameName)
  287. if (frameName == "End") then
  288.  
  289. local repeatAnim = currentAnim
  290. -- return to idle if finishing an emote
  291. if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
  292. repeatAnim = "idle"
  293. end
  294.  
  295. local animSpeed = currentAnimSpeed
  296. playAnimation(repeatAnim, 0.0, Humanoid)
  297. setAnimationSpeed(animSpeed)
  298. end
  299. end
  300.  
  301. -- Preload animations
  302. function playAnimation(animName, transitionTime, humanoid)
  303.  
  304. local roll = math.random(1, animTable[animName].totalWeight)
  305. local origRoll = roll
  306. local idx = 1
  307. while (roll > animTable[animName][idx].weight) do
  308. roll = roll - animTable[animName][idx].weight
  309. idx = idx + 1
  310. end
  311. -- print(animName .. " " .. idx .. " [" .. origRoll .. "]")
  312. local anim = animTable[animName][idx].anim
  313.  
  314. -- switch animation
  315. if (anim ~= currentAnimInstance) then
  316.  
  317. if (currentAnimTrack ~= nil) then
  318. currentAnimTrack:Stop(transitionTime)
  319. currentAnimTrack:Destroy()
  320. end
  321.  
  322. currentAnimSpeed = 1.0
  323.  
  324. -- load it to the humanoid; get AnimationTrack
  325. currentAnimTrack = humanoid:LoadAnimation(anim)
  326.  
  327. -- play the animation
  328. currentAnimTrack:Play(transitionTime)
  329. currentAnim = animName
  330. currentAnimInstance = anim
  331.  
  332. -- set up keyframe name triggers
  333. if (currentAnimKeyframeHandler ~= nil) then
  334. currentAnimKeyframeHandler:disconnect()
  335. end
  336. currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  337.  
  338. end
  339.  
  340. end
  341.  
  342. -------------------------------------------------------------------------------------------
  343. -------------------------------------------------------------------------------------------
  344.  
  345. local toolAnimName = ""
  346. local toolAnimTrack = nil
  347. local toolAnimInstance = nil
  348. local currentToolAnimKeyframeHandler = nil
  349.  
  350. function toolKeyFrameReachedFunc(frameName)
  351. if (frameName == "End") then
  352. -- print("Keyframe : ".. frameName)
  353. playToolAnimation(toolAnimName, 0.0, Humanoid)
  354. end
  355. end
  356.  
  357.  
  358. function playToolAnimation(animName, transitionTime, humanoid)
  359.  
  360. local roll = math.random(1, animTable[animName].totalWeight)
  361. local origRoll = roll
  362. local idx = 1
  363. while (roll > animTable[animName][idx].weight) do
  364. roll = roll - animTable[animName][idx].weight
  365. idx = idx + 1
  366. end
  367. -- print(animName .. " * " .. idx .. " [" .. origRoll .. "]")
  368. local anim = animTable[animName][idx].anim
  369.  
  370. if (toolAnimInstance ~= anim) then
  371.  
  372. if (toolAnimTrack ~= nil) then
  373. toolAnimTrack:Stop()
  374. toolAnimTrack:Destroy()
  375. transitionTime = 0
  376. end
  377.  
  378. -- load it to the humanoid; get AnimationTrack
  379. toolAnimTrack = humanoid:LoadAnimation(anim)
  380.  
  381. -- play the animation
  382. toolAnimTrack:Play(transitionTime)
  383. toolAnimName = animName
  384. toolAnimInstance = anim
  385.  
  386. currentToolAnimKeyframeHandler = toolAnimTrack.KeyframeReached:connect(toolKeyFrameReachedFunc)
  387. end
  388. end
  389.  
  390. function stopToolAnimations()
  391. local oldAnim = toolAnimName
  392.  
  393. if (currentToolAnimKeyframeHandler ~= nil) then
  394. currentToolAnimKeyframeHandler:disconnect()
  395. end
  396.  
  397. toolAnimName = ""
  398. toolAnimInstance = nil
  399. if (toolAnimTrack ~= nil) then
  400. toolAnimTrack:Stop()
  401. toolAnimTrack:Destroy()
  402. toolAnimTrack = nil
  403. end
  404.  
  405.  
  406. return oldAnim
  407. end
  408.  
  409. -------------------------------------------------------------------------------------------
  410. -------------------------------------------------------------------------------------------
  411.  
  412.  
  413. function onRunning(speed)
  414. if speed>0.01 then
  415. playAnimation("walk", 0.1, Humanoid)
  416. if currentAnimInstance and currentAnimInstance.AnimationId == "http://www.roblox.com/asset/?id=1949023689" then
  417. setAnimationSpeed(speed / 14.5)
  418. end
  419. pose = "Running"
  420. else
  421. if emoteNames[currentAnim] == nil then
  422. playAnimation("idle", 0.1, Humanoid)
  423. pose = "Standing"
  424. end
  425. end
  426. end
  427.  
  428. function onDied()
  429. pose = "Dead"
  430. end
  431.  
  432. function onJumping()
  433. playAnimation("jump", 0.1, Humanoid)
  434. jumpAnimTime = jumpAnimDuration
  435. pose = "Jumping"
  436. end
  437.  
  438. function onClimbing(speed)
  439. playAnimation("climb", 0.1, Humanoid)
  440. setAnimationSpeed(speed / 12.0)
  441. pose = "Climbing"
  442. end
  443.  
  444. function onGettingUp()
  445. pose = "GettingUp"
  446. end
  447.  
  448. function onFreeFall()
  449. if (jumpAnimTime <= 0) then
  450. playAnimation("fall", fallTransitionTime, Humanoid)
  451. end
  452. pose = "FreeFall"
  453. end
  454.  
  455. function onFallingDown()
  456. pose = "FallingDown"
  457. end
  458.  
  459. function onSeated()
  460. pose = "Seated"
  461. end
  462.  
  463. function onPlatformStanding()
  464. pose = "PlatformStanding"
  465. end
  466.  
  467. function onSwimming(speed)
  468. if speed>0 then
  469. pose = "Running"
  470. else
  471. pose = "Standing"
  472. end
  473. end
  474.  
  475. function getTool()
  476. for _, kid in ipairs(Figure:GetChildren()) do
  477. if kid.className == "Tool" then return kid end
  478. end
  479. return nil
  480. end
  481.  
  482. function getToolAnim(tool)
  483. for _, c in ipairs(tool:GetChildren()) do
  484. if c.Name == "toolanim" and c.className == "StringValue" then
  485. return c
  486. end
  487. end
  488. return nil
  489. end
  490.  
  491. function animateTool()
  492.  
  493. if (toolAnim == "None") then
  494. playToolAnimation("toolnone", toolTransitionTime, Humanoid)
  495. return
  496. end
  497.  
  498. if (toolAnim == "Slash") then
  499. playToolAnimation("toolslash", 0, Humanoid)
  500. return
  501. end
  502.  
  503. if (toolAnim == "Lunge") then
  504. playToolAnimation("toollunge", 0, Humanoid)
  505. return
  506. end
  507. end
  508.  
  509. function moveSit()
  510. RightShoulder.MaxVelocity = 0.15
  511. LeftShoulder.MaxVelocity = 0.15
  512. RightShoulder:SetDesiredAngle(3.14 /2)
  513. LeftShoulder:SetDesiredAngle(-3.14 /2)
  514. RightHip:SetDesiredAngle(3.14 /2)
  515. LeftHip:SetDesiredAngle(-3.14 /2)
  516. end
  517.  
  518. local lastTick = 0
  519.  
  520. function move(time)
  521. local amplitude = 1
  522. local frequency = 1
  523. local deltaTime = time - lastTick
  524. lastTick = time
  525.  
  526. local climbFudge = 0
  527. local setAngles = false
  528.  
  529. if (jumpAnimTime > 0) then
  530. jumpAnimTime = jumpAnimTime - deltaTime
  531. end
  532.  
  533. if (pose == "FreeFall" and jumpAnimTime <= 0) then
  534. playAnimation("fall", fallTransitionTime, Humanoid)
  535. elseif (pose == "Seated") then
  536. playAnimation("sit", 0.5, Humanoid)
  537. return
  538. elseif (pose == "Running") then
  539. playAnimation("walk", 0.1, Humanoid)
  540. elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  541. -- print("Wha " .. pose)
  542. stopAllAnimations()
  543. amplitude = 0.1
  544. frequency = 1
  545. setAngles = true
  546. end
  547.  
  548. if (setAngles) then
  549. desiredAngle = amplitude * math.sin(time * frequency)
  550.  
  551. RightShoulder:SetDesiredAngle(desiredAngle + climbFudge)
  552. LeftShoulder:SetDesiredAngle(desiredAngle - climbFudge)
  553. RightHip:SetDesiredAngle(-desiredAngle)
  554. LeftHip:SetDesiredAngle(-desiredAngle)
  555. end
  556.  
  557. -- Tool Animation handling
  558. local tool = getTool()
  559. if tool and tool:FindFirstChild("Handle") then
  560.  
  561. animStringValueObject = getToolAnim(tool)
  562.  
  563. if animStringValueObject then
  564. toolAnim = animStringValueObject.Value
  565. -- message recieved, delete StringValue
  566. animStringValueObject.Parent = nil
  567. toolAnimTime = time + .3
  568. end
  569.  
  570. if time > toolAnimTime then
  571. toolAnimTime = 0
  572. toolAnim = "None"
  573. end
  574.  
  575. animateTool()
  576. else
  577. stopToolAnimations()
  578. toolAnim = "None"
  579. toolAnimInstance = nil
  580. toolAnimTime = 0
  581. end
  582. end
  583.  
  584. -- connect events
  585. Humanoid.Died:connect(onDied)
  586. Humanoid.Running:connect(onRunning)
  587. Humanoid.Jumping:connect(onJumping)
  588. Humanoid.Climbing:connect(onClimbing)
  589. Humanoid.GettingUp:connect(onGettingUp)
  590. Humanoid.FreeFalling:connect(onFreeFall)
  591. Humanoid.FallingDown:connect(onFallingDown)
  592. Humanoid.Seated:connect(onSeated)
  593. Humanoid.PlatformStanding:connect(onPlatformStanding)
  594. Humanoid.Swimming:connect(onSwimming)
  595.  
  596. -- setup emote chat hook
  597. game.Players.LocalPlayer.Chatted:connect(function(msg)
  598. local emote = ""
  599. if msg == "/e dance" then
  600. emote = dances[math.random(1, #dances)]
  601. elseif (string.sub(msg, 1, 3) == "/e ") then
  602. emote = string.sub(msg, 4)
  603. elseif (string.sub(msg, 1, 7) == "/emote ") then
  604. emote = string.sub(msg, 8)
  605. end
  606.  
  607. if (pose == "Standing" and emoteNames[emote] ~= nil) then
  608. playAnimation(emote, 0.1, Humanoid)
  609. end
  610.  
  611. end)
  612.  
  613.  
  614. -- main program
  615.  
  616. local runService = game:service("RunService");
  617.  
  618. -- initialize to idle
  619. playAnimation("idle", 0.1, Humanoid)
  620. pose = "Standing"
  621.  
  622. while Figure.Parent~=nil do
  623. local _, time = wait(0.1)
  624. move(time)
  625. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement