Advertisement
Rip_Noname

Roblox Lua Sword Combat

May 10th, 2025
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.35 KB | None | 0 0
  1. -- Local Script (Animations)
  2. local plr = game.Players.LocalPlayer
  3. local char = plr.Character or plr.CharacterAdded:Wait()
  4. local humanoid = char:WaitForChild("Humanoid")
  5.  
  6. local RepStorage = game:GetService("ReplicatedStorage")
  7. local UIS = game:GetService("UserInputService")
  8. local Remotes = RepStorage:WaitForChild("Remotes")
  9.  
  10. local Sword = nil
  11. local Hit = 0
  12. local Cooldown = false
  13.  
  14. local MCD = false
  15. local MCD2 = 0.5
  16. local MHit = 0
  17.  
  18. -- Forward declaration of handleCombo
  19. local handleCombo
  20.  
  21. plr:WaitForChild("Weapons").ChildAdded:Connect(function(child)
  22.     Sword = child
  23. end)
  24.  
  25. if not Sword then
  26.     for _, v in plr:WaitForChild("Weapons"):GetChildren() do
  27.         Sword = v
  28.     end
  29. end
  30.  
  31. local function handleMele()
  32.     if MCD == true then return end
  33.  
  34.     for _, v in plr:WaitForChild("Weapons"):GetChildren() do
  35.         if v.Value == true then
  36.             Sword = v
  37.             handleCombo()
  38.             return
  39.         end
  40.     end
  41.  
  42.     local Mele1 = char.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(script.Mele.Mele1)
  43.     local Mele2 = char.Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(script.Mele.Mele2)
  44.  
  45.     MCD = true
  46.  
  47.     task.delay(MCD2, function()
  48.         MCD = false
  49.     end)
  50.  
  51.     if MHit == 0 then
  52.         MHit += 1
  53.         Mele1:Play()
  54.     elseif MHit == 1 then
  55.         MHit = 0
  56.         Mele2:Play()
  57.     end
  58.    
  59.     RepStorage:WaitForChild("Remotes").AttackRemote:FireServer("Mele")
  60.    
  61. end
  62.  
  63. -- Define handleCombo after forward declaration
  64. handleCombo = function()
  65.  
  66.     if not Sword then
  67.         handleMele()
  68.         return
  69.     end
  70.  
  71.     if Sword.Value == false then
  72.         handleMele()
  73.         return
  74.     end
  75.  
  76.     if Cooldown then
  77.         return
  78.     end
  79.  
  80.     -- Retrieve cooldown and animations
  81.     local CD = Sword:GetAttribute("Cooldown") or 1 -- Default cooldown
  82.     local Anim1 = script.Swords:FindFirstChild(Sword.Name) and script.Swords[Sword.Name]:FindFirstChild("M1Animation")
  83.     local Anim2 = script.Swords:FindFirstChild(Sword.Name) and script.Swords[Sword.Name]:FindFirstChild("M2Animation")
  84.  
  85.     if not Anim1 or not Anim2 then
  86.         warn("Animations missing for sword:", Sword.Name)
  87.         return
  88.     end
  89.  
  90.     Cooldown = true
  91.     Hit += 1
  92.  
  93.     local animator = humanoid:FindFirstChildOfClass("Animator")
  94.     if not animator then
  95.         warn("Animator not found in Humanoid")
  96.         return
  97.     end
  98.  
  99.     if Hit == 1 then
  100.         animator:LoadAnimation(Anim1):Play()
  101.     elseif Hit == 2 then
  102.         Hit = 0
  103.         animator:LoadAnimation(Anim2):Play()
  104.     end
  105.    
  106.     RepStorage:WaitForChild("Remotes").AttackRemote:FireServer(Sword)
  107.    
  108.     task.delay(Hit == 2 and 1.2 or CD, function()
  109.         Cooldown = false
  110.     end)
  111. end
  112.  
  113. UIS.InputBegan:Connect(function(input, gpe)
  114.     if gpe then return end
  115.     if input.UserInputType == Enum.UserInputType.MouseButton1 then
  116.         handleCombo()
  117.     end
  118. end)
  119.  
  120. -- Server Script (Combat)
  121. local RepStorage = game:GetService("ReplicatedStorage")
  122. local Debris = game:GetService("Debris")
  123.  
  124. local Remotes = RepStorage:WaitForChild("Remotes")
  125. local AttackRemote = Remotes:WaitForChild("AttackRemote")
  126.  
  127. local debouncedPlayers = {}
  128.  
  129. local function createHitbox(root, size, offset, duration)
  130.     local hitbox = Instance.new("Part")
  131.     hitbox.Size = size or Vector3.new(5, 5, 3)
  132.     hitbox.Transparency = 1
  133.     hitbox.CanCollide = false
  134.     hitbox.Anchored = false
  135.     hitbox.CFrame = root.CFrame * (offset or CFrame.new(0, 0, -2.5))
  136.     hitbox.Parent = workspace:WaitForChild("Hitboxes")
  137.  
  138.     local weld = Instance.new("WeldConstraint", hitbox)
  139.     weld.Part0 = hitbox
  140.     weld.Part1 = root
  141.  
  142.     Debris:AddItem(hitbox, duration or 0.1)
  143.     return hitbox
  144. end
  145.  
  146. local function applyKnockback(character, force, direction)
  147.     local rootPart = character:FindFirstChild("HumanoidRootPart")
  148.     if not rootPart then return end
  149.  
  150.     local bodyVelocity = Instance.new("BodyVelocity")
  151.     bodyVelocity.MaxForce = Vector3.new(100000, 100000, 100000)
  152.     bodyVelocity.Velocity = direction * force
  153.     bodyVelocity.P = 3000
  154.     bodyVelocity.Parent = rootPart
  155.  
  156.     Debris:AddItem(bodyVelocity, 0.1)
  157. end
  158.  
  159. local function highlightCharacter(character, color, duration)
  160.     local highlight = script.Highlight:Clone()
  161.     highlight.OutlineColor = color or Color3.fromRGB(217, 255, 161)
  162.     highlight.FillTransparency = 1
  163.     highlight.Parent = character
  164.  
  165.     Debris:AddItem(highlight, duration or 0.15)
  166. end
  167.  
  168. local function handleHit(hitCharacter, damage, knockbackForce, knockbackDirection)
  169.     local humanoid = hitCharacter:FindFirstChildOfClass("Humanoid")
  170.     if not humanoid then return end
  171.  
  172.     local sound = game.SoundService:FindFirstChild("SwordHit") -- Ensure the sound exists
  173.     if sound then
  174.         local swordHitSound = sound:Clone()
  175.         swordHitSound.Parent = hitCharacter:FindFirstChild("HumanoidRootPart") or hitCharacter
  176.         swordHitSound:Play()
  177.         Debris:AddItem(swordHitSound, 1) -- Cleanup after 1 second
  178.     end
  179.  
  180.     local highlightTemplate = script:FindFirstChild("Highlight")
  181.     if highlightTemplate then
  182.         local highlight = highlightTemplate:Clone()
  183.         highlight.Parent = hitCharacter
  184.         Debris:AddItem(highlight, 0.15) -- Remove after 0.15 seconds
  185.     end
  186.  
  187.     humanoid:TakeDamage(damage)
  188.     print(damage)
  189.    
  190.     applyKnockback(hitCharacter, knockbackForce, knockbackDirection)
  191. end
  192.  
  193. local function processHitbox(hitbox, attacker, damage, knockbackForce)
  194.     hitbox.Touched:Connect(function(hitPart)
  195.         local hitCharacter = hitPart.Parent
  196.         if not hitCharacter or not hitCharacter:FindFirstChild("Humanoid") then return end
  197.  
  198.         if hitCharacter:FindFirstChild("Unhitable") then
  199.             highlightCharacter(hitCharacter)
  200.             return
  201.         end
  202.  
  203.         if not debouncedPlayers[attacker] then
  204.             debouncedPlayers[attacker] = true
  205.  
  206.             local knockbackDirection = (hitCharacter.HumanoidRootPart.Position - attacker.HumanoidRootPart.Position).Unit
  207.             handleHit(hitCharacter, damage, knockbackForce, knockbackDirection)
  208.  
  209.             task.delay(0.15, function()
  210.                 debouncedPlayers[attacker] = nil
  211.             end)
  212.         end
  213.     end)
  214. end
  215.  
  216. local function startAttack(player, weapon)
  217.     local character = player.Character or player.CharacterAdded:Wait()
  218.     local root = character:FindFirstChild("HumanoidRootPart")
  219.  
  220.     if not root then return end
  221.  
  222.     local isMelee = (weapon == "Mele")
  223.     local damage = isMelee and 5 or (weapon:GetAttribute("BaseDamage") + weapon:GetAttribute("Mastery") / 20)
  224.     local knockbackForce = isMelee and 10 or 20
  225.  
  226.     if not isMelee then
  227.         local sound = game.SoundService.SwordSlash:Clone()
  228.         sound.Parent = root
  229.         sound:Destroy()
  230.     end
  231.  
  232.     local hitbox = createHitbox(root)
  233.     processHitbox(hitbox, character, damage, knockbackForce)
  234. end
  235.  
  236. AttackRemote.OnServerEvent:Connect(function(player, weapon)
  237.     startAttack(player, weapon)
  238. end)
  239.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement