Advertisement
Guest User

Animation Controller

a guest
Jul 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.05 KB | None | 0 0
  1. local module = {}
  2.  
  3. local Dependencies_Loaded = {}
  4.  
  5. local ModuleInfo = {
  6.     ["Name"] = "AnimationController",
  7.     ["Dependencies"] = {"Logging"}
  8. }
  9.  
  10. function module:getName()
  11.     return ModuleInfo.Name
  12. end
  13.  
  14.  
  15. function module:getDependencies()
  16.     return ModuleInfo.Dependencies
  17. end
  18.  
  19.  
  20. function module.addDependency(obj)
  21.     table.insert(Dependencies_Loaded, obj)
  22. end
  23.  
  24. local function gm(str)
  25.     for k,v in pairs(Dependencies_Loaded)do
  26.         if v.getName() == str then
  27.             return v
  28.         end
  29.     end
  30. end
  31.  
  32. spawn(function()
  33.     repeat wait() until #Dependencies_Loaded == #ModuleInfo.Dependencies
  34.     local Logging = gm("Logging")
  35.     Logging:Log(1, "Template loaded.")
  36. end)
  37.  
  38. -- [ REMOVE OLD ANIMATION SCRIPT ] --
  39. pcall(function()
  40.     game.StarterPlayer.StarterCharacterScripts:WaitForChild()("Animate"):Destroy()
  41.     local OldAnimations = shared.Humanoid:GetPlayingAnimationTracks()
  42.     for i,v in pairs(OldAnimations) do
  43.         v:Stop()
  44.     end
  45. end)
  46.  
  47. local Override = false
  48. local State = "Idle"
  49. local Action = "None"
  50. local ActionOverride = false
  51. local CrouchOverride = false
  52.  
  53. local Enabled = false
  54.  
  55. local Animations = nil
  56.  
  57. local UserInputService = game:GetService("UserInputService")
  58.  
  59. -- [ STRAFE SETUP ] --
  60. local crouched = false
  61. local w = false
  62. local a = false
  63. local s = false
  64. local d = false
  65.  
  66. -- [ ADD ANIMATIONS ] --
  67. function addAnimations()
  68.     local Gender = shared.Character:FindFirstChild("Gender")
  69.     if Gender.Value == "Male" then
  70.         Animations = {
  71.             ["WALK"] = nil,
  72.             ["RUNNING"] = nil,
  73.             ["IDLE"] = nil,
  74.             ["JUMP"] = nil,
  75.             ["FALLING"] = nil,
  76.             ["STRAFELEFT"] = nil,
  77.             ["STRAFERIGHT"] = nil,
  78.             ["CROUCHFORWARD"] = nil,
  79.             ["CROUCHLEFT"] = nil,
  80.             ["CROUCHBACK"] = nil,
  81.             ["CROUCHRIGHT"] = nil,
  82.             ["CROUCHIDLE"] = nil
  83.         }
  84.     elseif Gender.Value == "Female" then
  85.         Animations = {
  86.             ["WALK"] = nil,
  87.             ["RUNNING"] = nil,
  88.             ["IDLE"] = nil,
  89.             ["JUMP"] = nil,
  90.             ["FALLING"] = nil,
  91.             ["STRAFELEFT"] = nil,
  92.             ["STRAFERIGHT"] = nil,
  93.             ["CROUCHFORWARD"] = nil,
  94.             ["CROUCHLEFT"] = nil,
  95.             ["CROUCHBACK"] = nil,
  96.             ["CROUCHRIGHT"] = nil,
  97.             ["CROUCHIDLE"] = nil
  98.         }
  99.     end
  100. end
  101.  
  102. -- [ REMOVE ANIMATIONS ] --
  103. function removeAnimations()
  104.     Animations = nil
  105. end
  106.  
  107. -- [ ENABLE/DISABLE ANIMATIONS ] --
  108. function module:Enable()
  109.     Enabled = true
  110.     addAnimations()
  111. end
  112.  
  113. function module:Disable()
  114.     Enabled = false
  115.     removeAnimations()
  116.    
  117.     pcall(function()
  118.         local DisabledAnimations = shared.Humanoid:GetPlayingAnimationTracks()
  119.         for i,v in pairs(DisabledAnimations) do
  120.             v:Stop()
  121.         end
  122.     end)
  123. end
  124.  
  125. repeat wait() until Animations ~= nil
  126.  
  127. for j,k in pairs(Animations) do
  128.     local Animation = Instance.new("Animation")
  129.     Animation.AnimationId = k
  130.     Animations[j] = shared.Humanoid:LoadAnimation(Animation)
  131. end
  132.  
  133. function onKeyPress(actionName, userInputState, inputObject)
  134.     if userInputState == Enum.UserInputState.Begin and Enabled == true then
  135.         Override = true
  136.         if State == "Walking" then
  137.             State = "Running"
  138.         end
  139.     elseif userInputState == Enum.UserInputState.End and Enabled == true then
  140.         Override = false
  141.         if State == "Running" then
  142.             State = "Walking"
  143.         end
  144.     end
  145. end
  146.  
  147. game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.LeftShift)
  148.  
  149. UserInputService.InputBegan:connect(function(inputObject, gameProcessedEvent)
  150.     if inputObject.KeyCode == Enum.KeyCode.W then
  151.         w = true
  152.     end
  153.    
  154.     if inputObject.KeyCode == Enum.KeyCode.A then
  155.         a = true
  156.     end
  157.    
  158.     if inputObject.KeyCode == Enum.KeyCode.S then
  159.         s = true
  160.     end
  161.    
  162.     if inputObject.KeyCode == Enum.KeyCode.D then
  163.         d = true
  164.     end
  165.    
  166.     if inputObject.KeyCode == Enum.KeyCode.C then
  167.         crouched = true
  168.     end
  169. end)
  170.  
  171. UserInputService.InputEnded:connect(function(inputObject, gameProcessedEvent)
  172.     if inputObject.KeyCode == Enum.KeyCode.W then
  173.         w = false
  174.     end
  175.    
  176.     if inputObject.KeyCode == Enum.KeyCode.A then
  177.         a = false
  178.     end
  179.    
  180.     if inputObject.KeyCode == Enum.KeyCode.S then
  181.         s = false
  182.     end
  183.    
  184.     if inputObject.KeyCode == Enum.KeyCode.D then
  185.         d = false
  186.     end
  187.    
  188.     if inputObject.KeyCode == Enum.KeyCode.C then
  189.         crouched = false
  190.     end
  191. end)
  192.  
  193. spawn(function()
  194.     if a == true then
  195.         if crouched == false and not State == "Walking"  then
  196.             Animations.WALK:Stop()
  197.             Animations.STRAFELEFT:Play()
  198.         end
  199.     elseif a == false then
  200.         if Animations.STRAFELEFT.IsPlaying then
  201.             Animations.STRAFELEFT:Stop()
  202.         end
  203.     end
  204.     if d == true then
  205.         if crouched == false and not State == "Walking" then
  206.             Animations.WALK:Stop()
  207.             Animations.STRAFERUGHT:Play()
  208.         end
  209.     elseif d == false then
  210.         if Animations.STRAFERIGHT.IsPlaying then
  211.             Animations.STRAFERIGHT:Stop()
  212.         end
  213.     end
  214. end)
  215.  
  216. spawn(function()
  217.     if crouched == true then
  218.         Animations.WALK:Stop()
  219.         Animations.RUNNING:Stop()
  220.         if not w and not a and not s and not d then
  221.             Animations.CROUCHIDLE:Play()
  222.         end
  223.        
  224.         if w then
  225.             Animations.CROUCHFORWARD:Play()
  226.         end
  227.         if a then
  228.             Animations.CROUCHLEFT:Play()
  229.         end
  230.         if s then
  231.             Animations.CROUCHBACK:Play()
  232.         end
  233.         if d then
  234.             Animations.CROUCHRIGHT:Play()
  235.         end
  236.     end
  237. end)
  238.  
  239. if Enabled == true then
  240.     spawn(function()
  241.         repeat
  242.         wait()
  243.         if State == "Walking" and not Animations.WALK.IsPlaying then
  244.             Animations.WALK:Play()
  245.             Animations.RUNNING:Stop()
  246.             shared.Humanoid.WalkSpeed = 8
  247.         elseif State == "Running" and not Animations.RUNNING.IsPlaying and not CrouchOverride then
  248.             Animations.RUNNING:Play()
  249.             Animations.WALK:Stop()
  250.             shared.Humanoid.WalkSpeed = 26
  251.         elseif State == "Idle" then
  252.             Animations.WALK:Stop()
  253.             Animations.RUNNING:Stop()
  254.             if not Animations.IDLE.IsPlaying then
  255.                 Animations.IDLE:Play()
  256.             end
  257.         end
  258.         until 1+1==3
  259.     end)
  260. end
  261.  
  262. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement