Advertisement
TaylorsRus

Untitled

Jun 15th, 2023
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.35 KB | Source Code | 0 0
  1. local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
  2. local CombatHandler = Knit.CreateService {
  3.     Name = "CombatHandler",
  4.     Client = {}
  5. }
  6.  
  7. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  8. local Debris = game:GetService("Debris")
  9.  
  10. local Sounds = ReplicatedStorage:WaitForChild("Sounds").Combat
  11. local Config = ReplicatedStorage:WaitForChild("Config")
  12.  
  13. local CombatData = require(Config.Combat)
  14.  
  15. local PunchSound = Sounds:WaitForChild("Punch")
  16. local HitSound = Sounds:WaitForChild("Hit")
  17.  
  18. local STANDING_COMBO = CombatData.STANDING_COMBO
  19. local HIT_DELAY = CombatData.HIT_DELAY
  20. local HIT_DISTANCE = CombatData.HIT_DISTANCE
  21. local PUSH_MAGNITUDE = CombatData.PUSH_MAGNITUDE
  22. local PUSH_CLEANUP = HIT_DELAY / 2
  23. local END_COMBO = CombatData.END_COMBO
  24.  
  25. local InCombo = false
  26. local CurrentCombo = 0
  27. local StoredSpeed = 0
  28.  
  29. local StartTime = nil
  30. local LastAnimation = ""
  31.  
  32. local PlayersOnCooldown = {}
  33. local Animations = {
  34.     --[[
  35.         Add as many animations as you want, as long as it is composed as;
  36.         "Left/Right ..."
  37.     ]]
  38.     ["Right Swing"] = "",
  39.     ["Left Swing"] = "",
  40.     ["Right Kick"] = "",
  41.     ["Left Kick"] = "",
  42.     ["Left Roundhouse"] = "",  
  43.     ["Right Roundhouse"] = "", 
  44.     ["Right Uppercut"] = "",
  45.     ["Left Uppercut"] = "",
  46. }
  47.  
  48. local function DictionaryLength(Table)
  49.     local Count = 0
  50.     for _,_ in Table do
  51.         Count += 1
  52.     end
  53.     return Count
  54. end
  55.  
  56. local function RemoveValue(Haystack, Needle)
  57.     for Index, Value in ipairs(Haystack) do
  58.         if Value ~= Needle then continue end
  59.        
  60.         table.remove(Haystack, Index)
  61.     end
  62. end
  63.  
  64. local function RandomAnimation(Finisher: boolean)
  65.     math.randomseed(tick() % 1 * 1e4)
  66.     local LastCharacters = string.split(LastAnimation, " ")
  67.    
  68.     local AnimFound = false
  69.    
  70.     while not AnimFound do
  71.         local RandomIndex = math.random(1, DictionaryLength(Animations))
  72.         local Index = 0
  73.        
  74.         for Name, Track in pairs(Animations) do
  75.             local NewCharacters = string.split(Name, " ")
  76.            
  77.             local FoundIndex = Index == RandomIndex
  78.             local SameSide = LastCharacters[1] == NewCharacters[1]
  79.             if Name == LastAnimation or SameSide or not FoundIndex then Index += 1 continue end
  80.            
  81.             LastAnimation = Name
  82.             return Name, Track
  83.         end
  84.         task.wait()
  85.     end
  86. end
  87.  
  88. local function IsCharacter(Part: BasePart)
  89.     if not Part:FindFirstAncestor("LivingThings") then return false end
  90.     local CurrentPart = Part
  91.  
  92.     while CurrentPart do
  93.         if CurrentPart:IsA("Model") and CurrentPart:FindFirstChildOfClass("Humanoid") then
  94.             return true, CurrentPart
  95.         end
  96.         CurrentPart = CurrentPart.Parent
  97.     end
  98.    
  99.     return false
  100. end
  101.  
  102. local function RunHit(Player)
  103.     local Push = Instance.new("BodyVelocity")
  104.    
  105. end
  106.  
  107.  
  108. function CombatHandler:Hit(Player, KeyCode, InputType)
  109.     if not InputType or table.find(PlayersOnCooldown, Player) then return end
  110.     local Character = Player.Character
  111.     local Humanoid, HumRP = Character:WaitForChild("Humanoid"), Character:WaitForChild("HumanoidRootPart")
  112.    
  113.     if Humanoid:GetState() == Enum.HumanoidStateType.Running then
  114.         RunHit()
  115.     end
  116.    
  117.     local SwingName, SwingAnimation = RandomAnimation()
  118.     local Push = Instance.new("BodyVelocity")
  119.    
  120.     local RaycastParams = RaycastParams.new()
  121.     local Origin, Direction = HumRP.Position, (HumRP.CFrame.LookVector).Unit
  122.     RaycastParams.FilterDescendantsInstances = {Character}
  123.     RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
  124.    
  125.     if not InCombo then
  126.         InCombo = true
  127.         StoredSpeed = Humanoid.WalkSpeed
  128.     end
  129.    
  130.     Humanoid.WalkSpeed = 0
  131.     Humanoid.AutoRotate = false
  132.    
  133.     Push.MaxForce = Vector3.one * 1e4
  134.     Push.Velocity = Direction * PUSH_MAGNITUDE
  135.     Push.Parent = HumRP
  136.     Debris:AddItem(Push, PUSH_CLEANUP)
  137.    
  138.     PunchSound:Play()
  139.    
  140.     StartTime = os.clock()
  141.     task.spawn(function()
  142.         local DeltaTime = 0
  143.         repeat
  144.             DeltaTime = os.clock() - StartTime
  145.             task.wait()
  146.         until DeltaTime >= END_COMBO
  147.         if not InCombo then return end
  148.        
  149.         print("Combo ended!")
  150.         InCombo = false
  151.         Humanoid.WalkSpeed = StoredSpeed
  152.         Humanoid.AutoRotate = true
  153.     end)
  154.    
  155.     task.spawn(function()
  156.         local Result = workspace:Raycast(Origin, Direction * HIT_DISTANCE, RaycastParams)
  157.         if not Result then return end
  158.        
  159.         local ValidCharacter, Enemy = IsCharacter(Result.Instance)
  160.         if not ValidCharacter then return end
  161.        
  162.         print(Enemy.Name)
  163.     end)
  164.    
  165.     table.insert(PlayersOnCooldown, Player)
  166.     task.wait(HIT_DELAY)
  167.     RemoveValue(PlayersOnCooldown, Player)
  168. end
  169.  
  170. return CombatHandler
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement