Debounce being weird

So basically, I tested and my touched event won’t activate. I find the problem and it is the touched debounce. I think it is set to true after a certain amount of times used for some reason? I’m guessing it is because of the line before the last. I would like to know why and how this is happening? After 0.1 seconds debounce is set to true and when I set it to 0.2 it is completely fine.

script.Attack1.OnServerEvent:Connect(function(player, mouse)
	local touched = true
	touched = false
--some creation and hitbox
	Hitbox.Touched:Connect(function(Hit)
	if touched == false then		
Hit.Parent.Humanoid:TakeDamage(10)
		end
	end)
	wait(0.1)
	touched = true
		end)
1 Like

you are setting touched to true everytime this runs

the way you wrote it is kind of weird I would write it like this -

local touched = false

script.Attack1.OnServerEvent:Connect(function(player, mouse)
--some creation and hitbox
	Hitbox.Touched:Connect(function(Hit)
	if touched == false then		
        touched = true
           Hit.Parent.Humanoid:TakeDamage(10)
            wait(0.1)
             touched = false
		end
	end)

this should work without any errors

i don’t believe that is the case since it is a local value

Well then you believe wrong, the code @ASFNIN10DO posted is correct and what you should use

i have hidden some parts of the code since it would be pretty long so that’s not really the thing i was making, but the real problem that i believe is that the last line activates too early

You can change the time the debounce waits.

I think the real problem is every time the RemoteEvent “Attack1” fires it connects a new Touched event, by the looks of it that’s not what’s intended.

i don’t see why i can’t do 0.1 though without having it breaking

What do you mean “breaking” I need more context on what your code is supposed to do.

it creates a part on mouse that deals damage, after a bit it will stop dealing damage

Okay so what’s the problem?

Can I see your whole code? and can you explain clearly what you want and what’s not working?

1 Like

@batteryday Is correct. You are setting it to true each time, and that is the issue.

1 Like


script.Attack1.OnServerEvent:Connect(function(player, mouse)
local marked = {}
table.clear(marked)
local touched = true
touched = false
local Hitbox = Instance.new(“Part”, workspace)
game.Debris:AddItem(Hitbox, 2)
Hitbox.Transparency = 0
Hitbox.Material = Enum.Material.Neon
Hitbox.Shape = “Cylinder”
Hitbox.CastShadow = false
local rcol = math.random(1,2)
if rcol == 1 then Hitbox.BrickColor = BrickColor.new(“Lime green”) end
if rcol == 2 then Hitbox.BrickColor = BrickColor.new(“Black”) end
Hitbox.Size = Vector3.new(100, 5, 5)
Hitbox.Position = mouse
Hitbox.Orientation = Vector3.new(0,0,90)
CFrame.Angles(0,0,math.rad(90)) + parent.HumanoidRootPart.CFrame.lookVector * 30
Hitbox.Anchored = true
Hitbox.CanCollide = false
local TweenService = game:GetService(“TweenService”)
local goal = {}
goal.Size = Vector3.new(100, 10, 10)
goal.Transparency = 1
local tweenInfo = TweenInfo.new(2)
local tween = TweenService:Create(Hitbox, tweenInfo, goal)
tween:Play()
Hitbox.Touched:Connect(function(Hit)
if touched == false then
if table.find(marked, Hit.Parent, 1) then return end
if Hit.Parent == parent then return end
if not Hit or not Hit.Parent or not string.find(string.lower(Hit.name),“torso”) then return end
if not Hit.Parent:FindFirstChild(“Humanoid”) then return end
if Hit.Parent.ClassName == “Tool” then return end
local humanoid = Hit.Parent:FindFirstChild(“Humanoid”)
tagHumanoid(humanoid, vPlayer)
if not Hit.Parent:FindFirstChild(“Counter”) then
Hit.Parent.Humanoid:TakeDamage(10)
table.insert(marked, Hit.Parent)
wait(1)
untagHumanoid(humanoid)
return end
if Hit.Parent:FindFirstChild(“Counter”).Value == true then
table.insert(marked, Hit.Parent)
game.ServerStorage.GiveExp:Fire(10, humanoid.creator.Value)
wait(1)
untagHumanoid(humanoid)
return end
table.insert(marked, Hit.Parent, 1)
Hit.Parent.Humanoid:TakeDamage(10)
wait(1)
untagHumanoid(humanoid)
end
end)
wait(0.1)
print(marked)
touched = true
end)
pretty messy code

right now ill try your ideas. (30)

i put setting the touched to true outside of the remote event
, doesn’t seem to work, might be a studio bug?

i tried it in the actual game and it seems to work better but still fails in between.

my guy I can’t read this

no it’s not a studio bug, when something doesn’t work it doesn’t default to “studio bug”


It’s too difficult for me to diagnose and help you with your problem so I am resigning from this case - good luck

it’s a pretty understandable script but i guess it’s pretty messy in a lot of places. though “studio bug” might have been a bit wrong. I believe it is more of a delay.

It would help him a lot if you formatted it. It isn’t a delay, you are doing something wrong.

1 Like

What I am saying is that in order to help someone I’ll need to know exactly what they want and what is not working, and likely will need to see their script.

The script you posted is unreadable and I will not go through to format it. There simply is not enough context + readable code for me to try to diagnose the problem.


If you want to see what a proper debounce looks like:

local debounce = false

ScriptSignal:Connect(function(args)
    if debounce == false then
       debounce = true
       -- some code
       -- task.wait(time)
       debounce = false
    end
end)

This is as much as I can help you