Hello, I am currently making a sword system where when you swing the sword, it damages the enemy and plays a sound. But the problem is that the sword is having some weird bugs, Like when you first swing the sword, it does damage and sound only when you swing the sword and sword touches the other player. But after the first swing, the sword plays sound and does damage even when you don’t swing your sword, but it doesn’t happen before your first swing.
Video:
I have tried everything, but nothing seems to be working. Here are my scripts. Sorry if they are unorganized and unoptimized, I am still new to roblox studio and is not the best at scripting
Script One:
local DebounceTable = {}
local tool = script.Parent.Parent
tool.Activated:Connect(function()
script.Parent:WaitForChild("Part").Touched:Connect(function(objectThatTouchesTheHitbox)
if objectThatTouchesTheHitbox.Parent then
if objectThatTouchesTheHitbox.Parent:FindFirstChild("Humanoid") then
if DebounceTable[objectThatTouchesTheHitbox.Parent] == true then return end
DebounceTable[objectThatTouchesTheHitbox.Parent] = true
objectThatTouchesTheHitbox.Parent.Humanoid:TakeDamage(30)
wait(0.75)
DebounceTable[objectThatTouchesTheHitbox.Parent] = false
end
end
end)
end)
Script Two:
local tool = script.Parent
local hitbox = tool.Handle.Part
local animId = "rbxassetid://132940301793859"
local hitSoundId = "rbxassetid://935843979"
local cooldownTime = 0.75
local canActivate = true
tool.Activated:Connect(function()
if not canActivate then return end
canActivate = false
local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
local anim = Instance.new("Animation")
anim.AnimationId = animId
local animTrack = humanoid:LoadAnimation(anim)
animTrack.Priority = Enum.AnimationPriority.Action4
animTrack:Play()
end
task.wait(cooldownTime)
canActivate = true
end)
local hitSound = Instance.new("Sound")
hitSound.SoundId = hitSoundId
hitSound.Volume = 1
hitSound.Parent = tool.Handle
tool.Activated:Connect(function()
hitbox.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
print("Hit detected!")
if not canActivate then return end
-- Start the cooldown
canActivate = false
print("Playing sound!")
hitSound:Play()
task.wait(cooldownTime)
canActivate = true
end
end)
end)