Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Local Script (Animations)
- local plr = game.Players.LocalPlayer
- local char = plr.Character or plr.CharacterAdded:Wait()
- local humanoid = char:WaitForChild("Humanoid")
- local RepStorage = game:GetService("ReplicatedStorage")
- local UIS = game:GetService("UserInputService")
- local Remotes = RepStorage:WaitForChild("Remotes")
- local Sword = nil
- local Hit = 0
- local Cooldown = false
- local MCD = false
- local MCD2 = 0.5
- local MHit = 0
- -- Forward declaration of handleCombo
- local handleCombo
- plr:WaitForChild("Weapons").ChildAdded:Connect(function(child)
- Sword = child
- end)
- if not Sword then
- for _, v in plr:WaitForChild("Weapons"):GetChildren() do
- Sword = v
- end
- end
- local function handleMele()
- if MCD == true then return end
- for _, v in plr:WaitForChild("Weapons"):GetChildren() do
- if v.Value == true then
- Sword = v
- handleCombo()
- return
- end
- end
- local Mele1 = char.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(script.Mele.Mele1)
- local Mele2 = char.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(script.Mele.Mele2)
- MCD = true
- task.delay(MCD2, function()
- MCD = false
- end)
- if MHit == 0 then
- MHit += 1
- Mele1:Play()
- elseif MHit == 1 then
- MHit = 0
- Mele2:Play()
- end
- RepStorage:WaitForChild("Remotes").AttackRemote:FireServer("Mele")
- end
- -- Define handleCombo after forward declaration
- handleCombo = function()
- if not Sword then
- handleMele()
- return
- end
- if Sword.Value == false then
- handleMele()
- return
- end
- if Cooldown then
- return
- end
- -- Retrieve cooldown and animations
- local CD = Sword:GetAttribute("Cooldown") or 1 -- Default cooldown
- local Anim1 = script.Swords:FindFirstChild(Sword.Name) and script.Swords[Sword.Name]:FindFirstChild("M1Animation")
- local Anim2 = script.Swords:FindFirstChild(Sword.Name) and script.Swords[Sword.Name]:FindFirstChild("M2Animation")
- if not Anim1 or not Anim2 then
- warn("Animations missing for sword:", Sword.Name)
- return
- end
- Cooldown = true
- Hit += 1
- local animator = humanoid:FindFirstChildOfClass("Animator")
- if not animator then
- warn("Animator not found in Humanoid")
- return
- end
- if Hit == 1 then
- animator:LoadAnimation(Anim1):Play()
- elseif Hit == 2 then
- Hit = 0
- animator:LoadAnimation(Anim2):Play()
- end
- RepStorage:WaitForChild("Remotes").AttackRemote:FireServer(Sword)
- task.delay(Hit == 2 and 1.2 or CD, function()
- Cooldown = false
- end)
- end
- UIS.InputBegan:Connect(function(input, gpe)
- if gpe then return end
- if input.UserInputType == Enum.UserInputType.MouseButton1 then
- handleCombo()
- end
- end)
- -- Server Script (Combat)
- local RepStorage = game:GetService("ReplicatedStorage")
- local Debris = game:GetService("Debris")
- local Remotes = RepStorage:WaitForChild("Remotes")
- local AttackRemote = Remotes:WaitForChild("AttackRemote")
- local debouncedPlayers = {}
- local function createHitbox(root, size, offset, duration)
- local hitbox = Instance.new("Part")
- hitbox.Size = size or Vector3.new(5, 5, 3)
- hitbox.Transparency = 1
- hitbox.CanCollide = false
- hitbox.Anchored = false
- hitbox.CFrame = root.CFrame * (offset or CFrame.new(0, 0, -2.5))
- hitbox.Parent = workspace:WaitForChild("Hitboxes")
- local weld = Instance.new("WeldConstraint", hitbox)
- weld.Part0 = hitbox
- weld.Part1 = root
- Debris:AddItem(hitbox, duration or 0.1)
- return hitbox
- end
- local function applyKnockback(character, force, direction)
- local rootPart = character:FindFirstChild("HumanoidRootPart")
- if not rootPart then return end
- local bodyVelocity = Instance.new("BodyVelocity")
- bodyVelocity.MaxForce = Vector3.new(100000, 100000, 100000)
- bodyVelocity.Velocity = direction * force
- bodyVelocity.P = 3000
- bodyVelocity.Parent = rootPart
- Debris:AddItem(bodyVelocity, 0.1)
- end
- local function highlightCharacter(character, color, duration)
- local highlight = script.Highlight:Clone()
- highlight.OutlineColor = color or Color3.fromRGB(217, 255, 161)
- highlight.FillTransparency = 1
- highlight.Parent = character
- Debris:AddItem(highlight, duration or 0.15)
- end
- local function handleHit(hitCharacter, damage, knockbackForce, knockbackDirection)
- local humanoid = hitCharacter:FindFirstChildOfClass("Humanoid")
- if not humanoid then return end
- local sound = game.SoundService:FindFirstChild("SwordHit") -- Ensure the sound exists
- if sound then
- local swordHitSound = sound:Clone()
- swordHitSound.Parent = hitCharacter:FindFirstChild("HumanoidRootPart") or hitCharacter
- swordHitSound:Play()
- Debris:AddItem(swordHitSound, 1) -- Cleanup after 1 second
- end
- local highlightTemplate = script:FindFirstChild("Highlight")
- if highlightTemplate then
- local highlight = highlightTemplate:Clone()
- highlight.Parent = hitCharacter
- Debris:AddItem(highlight, 0.15) -- Remove after 0.15 seconds
- end
- humanoid:TakeDamage(damage)
- print(damage)
- applyKnockback(hitCharacter, knockbackForce, knockbackDirection)
- end
- local function processHitbox(hitbox, attacker, damage, knockbackForce)
- hitbox.Touched:Connect(function(hitPart)
- local hitCharacter = hitPart.Parent
- if not hitCharacter or not hitCharacter:FindFirstChild("Humanoid") then return end
- if hitCharacter:FindFirstChild("Unhitable") then
- highlightCharacter(hitCharacter)
- return
- end
- if not debouncedPlayers[attacker] then
- debouncedPlayers[attacker] = true
- local knockbackDirection = (hitCharacter.HumanoidRootPart.Position - attacker.HumanoidRootPart.Position).Unit
- handleHit(hitCharacter, damage, knockbackForce, knockbackDirection)
- task.delay(0.15, function()
- debouncedPlayers[attacker] = nil
- end)
- end
- end)
- end
- local function startAttack(player, weapon)
- local character = player.Character or player.CharacterAdded:Wait()
- local root = character:FindFirstChild("HumanoidRootPart")
- if not root then return end
- local isMelee = (weapon == "Mele")
- local damage = isMelee and 5 or (weapon:GetAttribute("BaseDamage") + weapon:GetAttribute("Mastery") / 20)
- local knockbackForce = isMelee and 10 or 20
- if not isMelee then
- local sound = game.SoundService.SwordSlash:Clone()
- sound.Parent = root
- sound:Destroy()
- end
- local hitbox = createHitbox(root)
- processHitbox(hitbox, character, damage, knockbackForce)
- end
- AttackRemote.OnServerEvent:Connect(function(player, weapon)
- startAttack(player, weapon)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement