Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my damage debounce script, hitting two times instead of once?

I made a damage script, that damages a player 20 + some other numbers. Then the game makes a gui that floats over the player that was hit. But for some reason, the debounce does not work properly and i hit more than 2-3 times instead of only once.

Strong.Touched:connect(function(hit)
if not debounce then return end
debounce = false
local opponent = hit.Parent:findFirstChild("Humanoid") or hit.Parent.Parent:findFirstChild("Humanoid")
        if opponent and opponent ~= player.Character.Humanoid then
        if opponent.Parent:findFirstChild("RicoStacks") then
opponent.Parent:findFirstChild("RicoStacks").Value = opponent.Parent:findFirstChild("RicoStacks").Value + 1
local dmg = math.floor(script.Parent:FindFirstChild("Charging").Value / 1.55 + baseAtk + 1 + opponent.Parent:findFirstChild("RicoStacks").Value/2)
           opponent:TakeDamage(dmg)
------------------------------------------------------
-----------------[Text/Gui and debounce]---------------------------
local effect = Instance.new("BillboardGui", opponent.Parent.Head)
effect.Size = UDim2.new(1,0,1,0)
wait()
    local text = Instance.new("TextBox")
    text.Parent = effect
    text.TextSize = 60
    text.TextColor3 = Color3.new(255,0,0)
    text.TextStrokeColor3 = Color3.new(170,0,0)
    text.Font = "Cartoon"
    text.BackgroundTransparency = 1
    text.Size = UDim2.new(1,0,1,0)


1 answer

✓ Accepted answer

Sorry, this answer doesn't have much content but when you create a variable "Debounce" when the part is touched, you just making 2 to 3 debounces. That's why it's hitting more than once.

To solve this problem you want to create a debounce outside the function

here's an example;

local Debounce = false
local part = script.Parent -- this is only an example

part.Touched:Connect(function(h)
	if Debounce == false then
		Debounce = true
		-- your code
	end
	wait()
	Debounce = false -- so you can fire again or do damage again
end)

The rest of your script looks fine, though make sure you make it neater and create variables for necessary objects.

If this helped your understand, learn or helped you then please accept answer ;)

Good luck and have fun developing!

This discussion is preserved as it was. New answers, comments, and votes are closed.