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

How to stop touch detection spamming?

I'm working on touch detection, but when I go to test it out, the detection spams between being touched, and the touch ending when I haven't stepped off? What can I do to stop this spam?

local buttonPressed = false

script.Parent.Touched:Connect(function(part)

	local parent = part.Parent



	if game.Players:GetPlayerFromCharacter(parent) and buttonPressed == false then
	
		local player = game.Players:GetPlayerFromCharacter(parent)
		player.PlayerGui.BuyMoves.Enabled = true
	end


	
	
end)




script.Parent.TouchEnded:Connect(function(part)
	
	
	local parent = part.Parent
	
	if game.Players:GetPlayerFromCharacter(parent) then
		buttonPressed = false
		local player = game.Players:GetPlayerFromCharacter(parent)
		player.PlayerGui.BuyMoves.Enabled = false
		
	end

	
end)

Thanks!

1 answer

✓ Accepted answer

Debounce

local debounce = false

something.Touched:connect(function()
	if debounce then return end
	debounce = true
	--code
	wait(.5)
	debounce = false
end)

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