wait(1)
local debounce = false
function Own(hit)
if debounce == false then
local player = hit.Parent:FindFirstChild("Humanoid")
if player then
local owner = Instance.new('StringValue', script.Parent)
local debounce = true
wait(2)
local debounce = false
end
end
end
script.Parent.Head.Touched:connect(Own)
How do I fix the debounce?
2 answers
Nevermind, I fixed it, this is the working script.
The problem with the first one was, after it was touched, alot of String Values would show up instead of 1.
wait(1)
local debounce = false
function Own(hit)
if not debounce then
debounce = true
local player = hit.Parent:FindFirstChild("Humanoid")
if player then
local owner = Instance.new('StringValue', script.Parent)
wait(2)
local debounce = false
else
return
end
end
end
script.Parent.Head.Touched:connect(Own)
Edit: I see that you've fixed your code, but you still may want to take a look at this. Using wait()/"blocking delays" isn't usually desirable in code.
Instead of using a true/false variable and wait(), try recording the last time the function was run (using time()), finding the difference between that time and the current time when the function is called, and then comparing that to a delay value. If the difference between the last time the function ran and the current time is greater than a debounce time, then run the function again and set the last ran time to the current one.
Hint:
if(time() - lastTimeRun > debounceTimeConstant)
lastTimeRun = time()
end
If you have any further questions, feel free to ask!
1 comment
Oh I see, thank you!
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
We need to know what is wrong with your script in order to help.
Please edit your post and add the console output