Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
- local CombatHandler = Knit.CreateService {
- Name = "CombatHandler",
- Client = {}
- }
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Debris = game:GetService("Debris")
- local Sounds = ReplicatedStorage:WaitForChild("Sounds").Combat
- local Config = ReplicatedStorage:WaitForChild("Config")
- local CombatData = require(Config.Combat)
- local PunchSound = Sounds:WaitForChild("Punch")
- local HitSound = Sounds:WaitForChild("Hit")
- local STANDING_COMBO = CombatData.STANDING_COMBO
- local HIT_DELAY = CombatData.HIT_DELAY
- local HIT_DISTANCE = CombatData.HIT_DISTANCE
- local PUSH_MAGNITUDE = CombatData.PUSH_MAGNITUDE
- local PUSH_CLEANUP = HIT_DELAY / 2
- local END_COMBO = CombatData.END_COMBO
- local InCombo = false
- local CurrentCombo = 0
- local StoredSpeed = 0
- local StartTime = nil
- local LastAnimation = ""
- local PlayersOnCooldown = {}
- local Animations = {
- --[[
- Add as many animations as you want, as long as it is composed as;
- "Left/Right ..."
- ]]
- ["Right Swing"] = "",
- ["Left Swing"] = "",
- ["Right Kick"] = "",
- ["Left Kick"] = "",
- ["Left Roundhouse"] = "",
- ["Right Roundhouse"] = "",
- ["Right Uppercut"] = "",
- ["Left Uppercut"] = "",
- }
- local function DictionaryLength(Table)
- local Count = 0
- for _,_ in Table do
- Count += 1
- end
- return Count
- end
- local function RemoveValue(Haystack, Needle)
- for Index, Value in ipairs(Haystack) do
- if Value ~= Needle then continue end
- table.remove(Haystack, Index)
- end
- end
- local function RandomAnimation(Finisher: boolean)
- math.randomseed(tick() % 1 * 1e4)
- local LastCharacters = string.split(LastAnimation, " ")
- local AnimFound = false
- while not AnimFound do
- local RandomIndex = math.random(1, DictionaryLength(Animations))
- local Index = 0
- for Name, Track in pairs(Animations) do
- local NewCharacters = string.split(Name, " ")
- local FoundIndex = Index == RandomIndex
- local SameSide = LastCharacters[1] == NewCharacters[1]
- if Name == LastAnimation or SameSide or not FoundIndex then Index += 1 continue end
- LastAnimation = Name
- return Name, Track
- end
- task.wait()
- end
- end
- local function IsCharacter(Part: BasePart)
- if not Part:FindFirstAncestor("LivingThings") then return false end
- local CurrentPart = Part
- while CurrentPart do
- if CurrentPart:IsA("Model") and CurrentPart:FindFirstChildOfClass("Humanoid") then
- return true, CurrentPart
- end
- CurrentPart = CurrentPart.Parent
- end
- return false
- end
- local function RunHit(Player)
- local Push = Instance.new("BodyVelocity")
- end
- function CombatHandler:Hit(Player, KeyCode, InputType)
- if not InputType or table.find(PlayersOnCooldown, Player) then return end
- local Character = Player.Character
- local Humanoid, HumRP = Character:WaitForChild("Humanoid"), Character:WaitForChild("HumanoidRootPart")
- if Humanoid:GetState() == Enum.HumanoidStateType.Running then
- RunHit()
- end
- local SwingName, SwingAnimation = RandomAnimation()
- local Push = Instance.new("BodyVelocity")
- local RaycastParams = RaycastParams.new()
- local Origin, Direction = HumRP.Position, (HumRP.CFrame.LookVector).Unit
- RaycastParams.FilterDescendantsInstances = {Character}
- RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
- if not InCombo then
- InCombo = true
- StoredSpeed = Humanoid.WalkSpeed
- end
- Humanoid.WalkSpeed = 0
- Humanoid.AutoRotate = false
- Push.MaxForce = Vector3.one * 1e4
- Push.Velocity = Direction * PUSH_MAGNITUDE
- Push.Parent = HumRP
- Debris:AddItem(Push, PUSH_CLEANUP)
- PunchSound:Play()
- StartTime = os.clock()
- task.spawn(function()
- local DeltaTime = 0
- repeat
- DeltaTime = os.clock() - StartTime
- task.wait()
- until DeltaTime >= END_COMBO
- if not InCombo then return end
- print("Combo ended!")
- InCombo = false
- Humanoid.WalkSpeed = StoredSpeed
- Humanoid.AutoRotate = true
- end)
- task.spawn(function()
- local Result = workspace:Raycast(Origin, Direction * HIT_DISTANCE, RaycastParams)
- if not Result then return end
- local ValidCharacter, Enemy = IsCharacter(Result.Instance)
- if not ValidCharacter then return end
- print(Enemy.Name)
- end)
- table.insert(PlayersOnCooldown, Player)
- task.wait(HIT_DELAY)
- RemoveValue(PlayersOnCooldown, Player)
- end
- return CombatHandler
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement