Debounce dont work instantly

when im checking touched parts and set true when parts exist, return statement says its false when i set it true.
local db = false
part.Touched:Connect(function()
db = true
end)
return db
output:
false

Edit: person above me has the answer. It’s because the print is printing before the denounce turns to true

1 Like

It depends on where you put the print, if you put it after the setting of db = true, then it will print true, however if you put it before the the setting of db = true, then it will print false, e.g above the event.

For example, this will print true:

local db = false
part.Touched:Connect(function()
     db = true
print(db)
end)
return db

also, I don’t really see a reason to be returning the value of db outside of the touched event

1 Like

print function is for the example, i have this script and when i getting return value, i getting false, only if i use wait(0.1) or else wait functions i can get true value.
In your example im getting false on return value, print is for debugging.

1 Like

I don’t think I understand what you’re saying, sorry.

When the system prints db’s value and it says it’s equal to true then the value has changed?

The return isn’t necessary because you already set the variable locally in the script, outside of the event. So even in the function it will set it globally in the script without the return, therefore it’s not necessary in this instance (not sure if that makes sense).

If you’re asking why the debounce is not working, you need to use an if statement to check the value of db, then set the value of db to true in the if statement. In order to set it back to false, you can either use a task.wait() in the if statement to wait for it to set back to false, or you can use another event to set it back to false, such as part.TouchEnded.

For example:

local db = false
part.Touched:Connect(function()
if db == false then
--insert what you want it to do here when touched
 db = true -- now that the debounce is activated, when the part is touched again
           -- it will not run the if statement because db will be equal to true
end
     
end)

I’m very confused. Where are you attempting to call this from? A module script? Script? LocalScript?

Im doing it in function and want to return db. Why i need if statement if db is default false and in part.Touched method im setting it to true. Its all in script fired by event.
1:


2022-07-24 13-36-02
2:
image
2022-07-24 13-30-58
3:

2022-07-24 13-38-33

All of them seem to work just fine; i dont see any issues. Assuming that you still want the debounce to work the second the player hits the character, just change the number in the “wait” section to 0.01. This seems to make it do immediately on my perspective. Then after “wait” do this code:

uAttackSomeone = true

I’m not sure if it works though

Its return me false anyway
image



but i found one issue. print in attack function working faster then in checkCollider function