Guys, I’m trying to make a sword combat system but I have a problem with a script.
local debounce1 = 1
UserInputService.InputBegan:Connect(function(input, gameProccessed)
local char = tool.Parent
humanoid = char:WaitForChild("Humanoid")
heavySlice1 = humanoid:LoadAnimation(tool:WaitForChild("HeavySlice1"))
heavySlice2 = humanoid:LoadAnimation(tool:WaitForChild("HeavySlice2"))
if input.UserInputType == Enum.UserInputType.MouseButton2 then
if debounce1 == 1 then
local waitTime = math.max(heavySlice1.Length, HeavyCooldownTime)
if not isAttacking then
isAttacking = true -- Disable repeated attack attempts during animation
canDamageHeavy = true
heavySlice1:Play() -- Play animation
wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
isAttacking = false -- Re-Enable attack attempts
debounce = 2
end
elseif debounce1 == 2 then
local waitTime = math.max(heavySlice2.Length, HeavyCooldownTime)
if not isAttacking then
isAttacking = true -- Disable repeated attack attempts during animation
canDamageHeavy = true
heavySlice2:Play() -- Play animation
wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
isAttacking = false -- Re-Enable attack attempts
debounce = 2
end
end
end
end)
The problem is that the sword animation doesn’t play and the otherHumanoid doesn’t take damage.
Here is the full script:
-- Settings
local CooldownTime = 0.5
local HeavyCooldownTime = 2
local Damage = 30
local HeavyDamage = 50
-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local UserInputService = game:GetService("UserInputService")
-- Tool Variables
local tool = script.Parent
local swordBlade = tool.Handle
-- Animations, Humanoid, Player
local humanoid, attack1, attack2, attack3, attack4, attack5, heavySlice1, heavySlice2, equip, player
-- Bool Values
local canDamage = true
local canDamageHeavy = true
local isAttacking = false
-- Equip Function
local function onEquip()
-- Set humanoid, animations and player variables
local char = tool.Parent
humanoid = char:WaitForChild("Humanoid")
player = Players:GetPlayerFromCharacter(char)
equip = humanoid:LoadAnimation(tool:WaitForChild("Equip"))
equip:Play()
end
-- Detect Hit Function
local function onDetectHit(otherPart)
local partParent = otherPart.Parent
local otherHumanoid = partParent:FindFirstChildWhichIsA("Humanoid")
-- Make sure blade doesn't harm controlling player
if otherHumanoid and otherHumanoid == humanoid then
return
elseif otherHumanoid and isAttacking and canDamage then
canDamage = false
otherHumanoid:TakeDamage(Damage)
elseif otherHumanoid and isAttacking and canDamageHeavy then
canDamageHeavy = false
otherHumanoid:TakeDamage(HeavyDamage)
end
end
-- On Attack Function
local debounce = 1
local function onAttack()
local char = tool.Parent
humanoid = char:WaitForChild("Humanoid")
attack1 = humanoid:LoadAnimation(tool:WaitForChild("Attack1"))
attack2 = humanoid:LoadAnimation(tool:WaitForChild("Attack2"))
attack3 = humanoid:LoadAnimation(tool:WaitForChild("Attack3"))
attack4 = humanoid:LoadAnimation(tool:WaitForChild("Attack4"))
attack5 = humanoid:LoadAnimation(tool:WaitForChild("Attack5"))
if debounce == 1 then
-- Attack 1
local waitTime = math.max(attack1.Length, CooldownTime)
if not isAttacking then
isAttacking = true -- Disable repeated attack attempts during animation
canDamage = true
attack1:Play() -- Play animation
wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
isAttacking = false -- Re-Enable attack attempts
debounce = 2
end
elseif debounce == 2 then
-- Attack 2
local waitTime = math.max(attack2.Length, CooldownTime)
if not isAttacking then
isAttacking = true -- Disable repeated attack attempts during animation
canDamage = true
attack2:Play() -- Play animation
wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
isAttacking = false -- Re-Enable attack attempts
debounce = 3
end
elseif debounce == 3 then
-- Attack 3
local waitTime = math.max(attack3.Length, CooldownTime)
if not isAttacking then
isAttacking = true -- Disable repeated attack attempts during animation
canDamage = true
attack3:Play() -- Play animation
wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
isAttacking = false -- Re-Enable attack attempts
debounce = 4
end
elseif debounce == 4 then
-- Attack 4
local waitTime = math.max(attack4.Length, CooldownTime)
if not isAttacking then
isAttacking = true -- Disable repeated attack attempts during animation
canDamage = true
attack4:Play() -- Play animation
wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
isAttacking = false -- Re-Enable attack attempts
debounce = 5
end
elseif debounce == 5 then
-- Attack 5
local waitTime = math.max(attack5.Length, CooldownTime)
if not isAttacking then
isAttacking = true -- Disable repeated attack attempts during animation
canDamage = true
attack5:Play() -- Play animation
wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
isAttacking = false -- Re-Enable attack attempts
debounce = 1
end
end
end
-- Heavy Hit Function
local debounce1 = 1
UserInputService.InputBegan:Connect(function(input, gameProccessed)
local char = tool.Parent
humanoid = char:WaitForChild("Humanoid")
heavySlice1 = humanoid:LoadAnimation(tool:WaitForChild("HeavySlice1"))
heavySlice2 = humanoid:LoadAnimation(tool:WaitForChild("HeavySlice2"))
if input.UserInputType == Enum.UserInputType.MouseButton2 then
if debounce1 == 1 then
local waitTime = math.max(heavySlice1.Length, HeavyCooldownTime)
if not isAttacking then
isAttacking = true -- Disable repeated attack attempts during animation
canDamageHeavy = true
heavySlice1:Play() -- Play animation
wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
isAttacking = false -- Re-Enable attack attempts
debounce1 = 2
end
elseif debounce1 == 2 then
local waitTime = math.max(heavySlice2.Length, HeavyCooldownTime)
if not isAttacking then
isAttacking = true -- Disable repeated attack attempts during animation
canDamageHeavy = true
heavySlice2:Play() -- Play animation
wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
isAttacking = false -- Re-Enable attack attempts
debounce1 = 2
end
end
end
end)
-- Connections
tool.Equipped:Connect(onEquip)
tool.Activated:Connect(onAttack)
swordBlade.Touched:Connect(onDetectHit)
I tried getting help from Roblox Studio Communities.