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

script keeps running multiple times even with debounce?

local debounce = false
local debounce2 = false
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not debounce then
		debounce = true
		print("debounce active")
		local char = hit.Parent
		print(char.Name)
		local chardata = game.ServerScriptService.DataCore:FindFirstChild(char.Name)
		if chardata and not debounce2 then
			debounce2 = true
			print("giving gems to"..char.Name)
			chardata.Gems.Value = chardata.Gems.Value + 10
			print("gave 10 gems to"..char.Name)
			wait()
			script.Parent.Parent:Destroy()
		end
	end
	
end)

this script adds 10 "gems" to a folder owned by the player. but for some reason it keeps running more than once resulting in 30 or 40 gems being added. I added a second debounce to try and prevent it from happening but regardless it continues to be a problem.

the script does fully run, but it runs fully more than once.

1 answer

✓ Accepted answer

model ur code like this:

local debounce = false
local dbLength = 3 -- waits 3 seconds
script.Parent.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		--put the rest of the code here
		--   ...

		wait(dbLength )
		debounce = false
	else
		print("debounce is true")
		--code  to run if debounce is true
	end
end)

that should work, good luck!

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