Advertisement
ailenkai

Combat System Script

Sep 23rd, 2023 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.52 KB | None | 0 0
  1. local UIS = game:GetService("UserInputService")
  2. local RS = game:GetService("ReplicatedStorage")
  3. local TS = game:GetService("TweenService")
  4. local rs = game:GetService('RunService')
  5. local GUIS = game:GetService("GuiService")
  6. local Folder = RS.Combat
  7. local Animate = Folder.Animate
  8. local Block = Folder.Block
  9. local CreateHitbox = Folder.CreateHitbox
  10.  
  11. local Player = game:GetService("Players").LocalPlayer
  12. local PlayerGui = Player.PlayerGui
  13. local Hud = PlayerGui:FindFirstChild("HUD")
  14. local HudMain = Hud:FindFirstChild("Main")
  15. local HudPower = HudMain:FindFirstChild("Power")
  16. local HudHealth = HudMain:FindFirstChild("Health")
  17. local HudStamina = HudMain:FindFirstChild("Stamina")
  18. local Character = Player.Character or Player.CharacterAdded:Wait()
  19. local Humanoid = Character:FindFirstChildOfClass("Humanoid")
  20. local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
  21. local VectorForce = HumanoidRootPart:FindFirstChild("VectorForce")
  22. local Animations = Character:WaitForChild("Animations")
  23. local Config = require(Character:WaitForChild("PlayerConfig"))
  24. local Camera = game.Workspace.CurrentCamera
  25.  
  26. local isReady = Config.isReady
  27. local isBlocking = Config.isBlocking
  28. local isAttacking = Config.isAttacking
  29. local currentSequence = Config.currentSequence
  30. local lastAttackTick = Config.lastAttackTick
  31. local lastBlockCooldownTick = Config.lastBlockCooldownTick
  32. local lastSlideTick = Config.lastSlideTick
  33. local lastDodgeTick = Config.lastDodgeTick
  34. local lshift = Config.lshift
  35. local q = Config.a
  36. local e = Config.d
  37. local isSliding = Config.isSliding
  38. local isJumping = Config.isJumping
  39. local isFalling = Config.isFalling
  40. local isStanding = Config.isStanding
  41. local isDodging = Config.isDodging
  42.  
  43.  
  44. --00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00
  45.  
  46. local easingtime = 0.1 --0~1, lower = smoother and slower
  47. local walkspeeds = Config.walkspeeds
  48.  
  49. local attackSequence = {
  50.     Animations.fistpunch1,
  51.     Animations.fistpunch2,
  52.     Animations.fistpunch3,
  53.     Animations.fistpunch4
  54. }
  55.  
  56. local Idle = Animations.fistidle
  57. local Block = Animations.fistblock
  58. local Slide = Animations.slide
  59. local DodgeLeft = Animations.dodgeleft
  60. local DodgeRight = Animations.dodgeright
  61.  
  62. --00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00
  63.  
  64. function roundNumber(num, numDecimalPlaces)
  65.     return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
  66. end
  67.  
  68. function lerp(a, b, t)
  69.     return a * (1-t) + (b*t)
  70. end
  71.  
  72. function shiftLock(active) --Toggle shift.lock function
  73.     if active then     
  74.        
  75.         Config.AutoRotate = false
  76.         Humanoid.AutoRotate = Config.AutoRotate
  77.         rs:UnbindFromRenderStep("ShiftLockCamera")
  78.         rs:BindToRenderStep("ShiftLock", Enum.RenderPriority.Character.Value, function()
  79.             UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
  80.             Humanoid.CameraOffset = Vector3.new(lerp(Humanoid.CameraOffset.X, 1.75, .05),0,0)
  81.             Camera.FieldOfView = lerp(Camera.FieldOfView, 60,0.05)
  82.             local _, y = workspace.CurrentCamera.CFrame.Rotation:ToEulerAnglesYXZ()
  83.             HumanoidRootPart.CFrame = HumanoidRootPart.CFrame:Lerp(CFrame.new(HumanoidRootPart.Position) * CFrame.Angles(0,y,0),.2)
  84.         end)
  85.        
  86.     else
  87.         rs:UnbindFromRenderStep("ShiftLock") -- Allow mouse to move freely.
  88.         rs:BindToRenderStep("ShiftLockCamera", Enum.RenderPriority.Camera.Value, function()
  89.             Humanoid.CameraOffset = Vector3.new(lerp(Humanoid.CameraOffset.X, 0, .05),0,0) --Move the camera back to normal.
  90.             Camera.FieldOfView = lerp(Camera.FieldOfView, 70,0.05)
  91.         end)
  92.         Humanoid.Changed:Connect(function(CameraOffset)
  93.             local found = false
  94.             if Humanoid.CameraOffset.X == 0 and not found then
  95.                 found = true
  96.                 rs:UnbindFromRenderStep("ShiftLockCamera")
  97.                 Humanoid.CameraOffset = Vector3.new(0,0,0)
  98.             end
  99.         end)
  100.         UIS.MouseBehavior = Enum.MouseBehavior.Default -- Let the mouse move freely
  101.         Config.AutoRotate = true
  102.         Humanoid.AutoRotate = Config.AutoRotate
  103.        
  104.     end
  105. end
  106.  
  107. shiftLock(false)
  108.  
  109. GUIS.MenuOpened:Connect(function()
  110.     shiftLock(false)
  111. end)
  112. GUIS.MenuClosed:Connect(function()
  113.     if isReady then
  114.         shiftLock(true)
  115.     end
  116. end)
  117.  
  118. --00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00
  119.  
  120. local function checkState()
  121.     if Humanoid:GetState() == Enum.HumanoidStateType.Jumping then
  122.         isJumping = true
  123.         isFalling = false
  124.         isStanding = false
  125.     elseif Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
  126.         isJumping = false
  127.         isFalling = true
  128.         isStanding = false
  129.     else
  130.         isJumping = false
  131.         isFalling = false
  132.         isStanding = true
  133.     end
  134. end
  135.  
  136. Humanoid.StateChanged:Connect(checkState)
  137.  
  138. --00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00
  139.  
  140. -- Animate:FireServer(animationObj, animationSpeed, isRunning)
  141.  
  142. connectionBegan = UIS.InputBegan:Connect(function(input, gameProcessed)
  143.     if input.UserInputType == Enum.UserInputType.Keyboard then
  144.        
  145.         --ReadyStance
  146.         if input.KeyCode == Config.MeleeActivationKey then
  147.             if (tick() - Config.lastActivation) > Config.ActivationSpeed then
  148.                 if not isReady then
  149.                     isReady = true
  150.                     shiftLock(true)
  151.                     Animate:FireServer(Idle, 1, true)
  152.                     Config.lastActivation = tick()
  153.                 else
  154.                     Animate:FireServer(Idle, 1, false)
  155.                     shiftLock(false)
  156.                     --rs:UnbindFromRenderStep("ShiftLockCamera")
  157.                     isReady = false
  158.                     isBlocking = false
  159.                     isSliding = false
  160.                     Config.lastActivation = tick()
  161.                 end
  162.             end
  163.            
  164.         end
  165.        
  166.         -- Left Shift
  167.         if input.KeyCode == Enum.KeyCode.LeftShift then
  168.             lshift = true
  169.         end
  170.        
  171.         --Dodge Left/ Q
  172.         if input.KeyCode == Enum.KeyCode.Q then
  173.             q = true
  174.             if not isBlocking and isReady then
  175.                 if (tick() - lastDodgeTick) > Config.DodgeSpeed then
  176.                     isDodging = true
  177.                     Animate:FireServer(DodgeLeft,1,true)
  178.                     lastDodgeTick = tick()
  179.                 end
  180.             end
  181.         end
  182.  
  183.         --Dodge Right/ E
  184.         if input.KeyCode == Enum.KeyCode.E then
  185.             e = true
  186.             if not isBlocking and isReady then
  187.                 if (tick() - lastDodgeTick) > Config.DodgeSpeed then
  188.                     isDodging = true
  189.                     Animate:FireServer(DodgeRight,1,true)
  190.                     lastDodgeTick = tick()
  191.                 end
  192.             end
  193.         end
  194.        
  195.         -- slide
  196.         if input.KeyCode == Enum.KeyCode.LeftControl then
  197.             if not isBlocking and not isAttacking and not isSliding then
  198.                 if (tick() - lastSlideTick) > Config.SlideSpeed then
  199.                     isSliding = true
  200.                     Animate:FireServer(Slide, 1, true)
  201.                     lastSlideTick = tick()
  202.                 end
  203.             end
  204.         end
  205.        
  206.     end
  207.    
  208.     --Attacking
  209.     if input.UserInputType == Enum.UserInputType.MouseButton1 then
  210.         if isReady and not isAttacking and not isBlocking and not isSliding then
  211.             if (tick() - lastAttackTick) > Config.AttackSpeed then
  212.                 isAttacking = true
  213.                 currentSequence = currentSequence + 1
  214.                 if currentSequence >= (#attackSequence + 1) then
  215.                     currentSequence = 1
  216.                 end
  217.                 Animate:FireServer(attackSequence[currentSequence], 2, true)
  218.                 CreateHitbox:FireServer(attackSequence[currentSequence],0.1)
  219.                 print(currentSequence)
  220.             end
  221.            
  222.         end
  223.     end
  224.    
  225.     --Blocking
  226.     if input.UserInputType == Enum.UserInputType.MouseButton2 then
  227.         if isReady and not isBlocking and not isAttacking and not isSliding then
  228.             if (tick() - lastBlockCooldownTick) > Config.BlockSpeed then
  229.                 isBlocking = true
  230.                 Animate:FireServer(Block, 2, true)
  231.             end
  232.         end
  233.     end
  234.    
  235.    
  236. end)
  237.  
  238. --00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00
  239.  
  240. connectionEnded = UIS.InputEnded:Connect(function(input, gameProcessed)
  241.    
  242.     -- Stopped Blocking
  243.     if input.UserInputType == Enum.UserInputType.MouseButton2 then
  244.         if isReady and isBlocking then
  245.             Animate:FireServer(Block, 1, false)
  246.             Animate:FireServer(Idle, 1, true)
  247.             lastBlockCooldownTick = tick()
  248.             isBlocking = false
  249.         end
  250.     end
  251.    
  252.     -- Stopped Attacking
  253.     if input.UserInputType == Enum.UserInputType.MouseButton1 then
  254.         if isReady and isAttacking then
  255.             lastAttackTick = tick()
  256.             isAttacking = false
  257.         end
  258.     end
  259.    
  260.     -- Stopped Sprinting
  261.     if input.UserInputType == Enum.UserInputType.Keyboard then
  262.         if input.KeyCode == Enum.KeyCode.LeftShift then
  263.             lshift = false
  264.         end
  265.     end
  266.    
  267.     -- Stopped Sliding
  268.     if input.UserInputType == Enum.UserInputType.Keyboard then
  269.         if input.KeyCode == Enum.KeyCode.LeftControl then
  270.             isSliding = false
  271.             Animate:FireServer(Slide, 1, false)
  272.             if not isBlocking and isReady then
  273.                 Animate:FireServer(Idle, 1, true)
  274.             end
  275.         end
  276.     end
  277.    
  278.     -- Stopped Dodge Left
  279.     if input.UserInputType == Enum.UserInputType.Keyboard then
  280.         if input.KeyCode == Enum.KeyCode.Q then
  281.             q = false
  282.             Animate:FireServer(DodgeLeft, 1, false)
  283.             if not isBlocking and isReady then
  284.                 Animate:FireServer(Idle, 1, true)
  285.             end
  286.         end
  287.     end
  288.    
  289.     -- Stopped Dodge Right
  290.     if input.UserInputType == Enum.UserInputType.Keyboard then
  291.         if input.KeyCode == Enum.KeyCode.E then
  292.             e = false
  293.             Animate:FireServer(DodgeRight, 1, false)
  294.             if not isBlocking and isReady then
  295.                 Animate:FireServer(Idle, 1, true)
  296.             end
  297.         end
  298.     end
  299.    
  300. end)
  301.  
  302. --00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00--00
  303.  
  304. rs:BindToRenderStep("Movement", Enum.RenderPriority.Character.Value, function()
  305.    
  306.     if Character and HumanoidRootPart and Humanoid and Humanoid.Health > 0 then
  307.         HudStamina.Size = UDim2.new( (Humanoid.MaxHealth/Humanoid.Health) *0.25, 0,0.01,0 )
  308.        
  309.         local HumanoidRootPartSpeed = roundNumber(HumanoidRootPart.Velocity.magnitude,1)
  310.        
  311.         --[[ FOR FIRST PERSON
  312.         local inFirstPerson = (Camera.CoordinateFrame.p - Character.Head.Position).Magnitude
  313.  
  314.         if inFirstPerson < .51 then
  315.             for _, v in pairs(Character:GetChildren())do
  316.                 if v:IsA"Part" and v.Name ~= "Head" and v.Name ~= "Torso" then
  317.                     v.LocalTransparencyModifier = 0
  318.                 end
  319.             end
  320.             --Humanoid.CameraOffset = Vector3.new(0,0,0)
  321.         end]]
  322.        
  323.         -- Running and Sliding
  324.         if lshift and not isSliding then
  325.             Humanoid.WalkSpeed = lerp(Humanoid.WalkSpeed,Humanoid.WalkSpeed + (walkspeeds.runningspeed - Humanoid.WalkSpeed),easingtime)
  326.         elseif not isSliding then
  327.             Humanoid.WalkSpeed = lerp(Humanoid.WalkSpeed,Humanoid.WalkSpeed + (walkspeeds.walkingspeed - Humanoid.WalkSpeed),easingtime)
  328.         elseif isSliding then
  329.             Humanoid.WalkSpeed = 0
  330.         end
  331.        
  332.         if isFalling then
  333.             VectorForce.Force = Vector3.new(0,0,-( (HumanoidRootPartSpeed * 4)/2 ))
  334.         elseif isJumping then
  335.             VectorForce.Force = Vector3.new(0,0,-( (HumanoidRootPartSpeed * 7)/3 ))
  336.         elseif isStanding then
  337.             VectorForce.Force = Vector3.new(0,0,-( (HumanoidRootPartSpeed * 2)/3 ))
  338.         end
  339.        
  340.     else
  341.         rs:UnbindFromRenderStep("Movement")
  342.         return warn(Character.Name.. " died")
  343.     end
  344.  
  345. end)
  346.  
  347.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement