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.
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
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)