My sword's hitbox is damaging twice on one click

Hey Robloxians,
I am working on a sword game, while making sword I added an hitbox that will damage on click and I want it to be disable after 1 or 0.5 seconds of click so it doesn’t do that touch interest damage. But somehow even after the script being disabled, my hitbox is doing double, triple and sometimes quadruple damage in one click. Here is my script:

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	local debounce = false
	
		script.Parent.Parent.SwordDamage:Play()
		humanoid:TakeDamage(10)
		script.Disabled = true
	
end)

Please tell me how do I make it do damage once per click and then the script becomes disable so it doesn’t attack.

1 Like

I’ve also tried debounce and it didn’t work so don’t mind that debounce variable there

The script is most likely doing this because the sword or player is still most likely moving and triggering the script
I think you need to add an if statement at the start of the function checking if the debounce variable is set to true first then add a wait() and set the debounce back to false when it’s done

local debounce = true
script.Parent.Touched:Connect(function(hit)
    if debounce == true then
        debounce = false
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
		script.Parent.Parent.SwordDamage:Play()
		humanoid:TakeDamage(10)
        wait([placeholder])
        debounce = true
   end
end)

You’ll have to adjust the script to your liking but that’s it ig

Edit:

Sorry, didn’t see that. Still, try it just in case

1 Like

Hey, it worked but now it made the hitbox working even when the sword is just touched and not used, any solutions?

I’m thinking CanTouch could be a solution, so like you keep CanTouch off on the hitbox so when the tool is activated, you turn it on

1 Like

It worked! Thanks!!! I really appreciate you help!

1 Like
local debounce = true
script.Parent.Touched:Connect(function(hit)
    if debounce == true and script.Parent.Parent:FindFirstChild("Humanoid") then
        debounce = false
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
		script.Parent.Parent.SwordDamage:Play()
		humanoid:TakeDamage(10)
        wait([placeholder])
        debounce = true
   end
end)
1 Like

oh i am sorry you found the solution
what i tried is by checking if its used by a character

Its okay, thanks for trying to help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.