Seeking Exact Debounce to avoid mashed Event triggers

Hello, if you have read my previous help request on how to create a Shop Field, I thank those who have attempted to help solve my problem. On my own time, I have managed to create a simpler solution which is nearly done, my final problem is debouncing.

local OpenShop = game.ReplicatedStorage.OpenShop
local CloseShop = game.ReplicatedStorage.CloseShop

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if not debounce and game.Players:GetPlayerFromCharacter(hit.Parent) then
		debounce = true
		wait(1)
		print("Welcome!")
		OpenShop:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
	end
end)

script.Parent.TouchEnded:Connect(function(miss)
	
	if debounce and miss then
		debounce = false
		wait(1)
		print("Goodbye!")
		CloseShop:FireClient(game.Players:GetPlayerFromCharacter(miss.Parent))
		wait(1)
	end
	
end)

Functionally, this script works in the sense of operating the RemoteEvent and summoning and dismissing the GUI, but due to the nature of using a brick-based field, it often registers a Touch or TouchEnded multiple times at once which can often break the GUI.

My question is that if there’s a debounce method which ensures that the Touch and TouchEnded events only work when the player has completely entered and left the field without any mashed triggers?

I don’t think .TouchEnded is always reliable. I’ve read your previous post and I think a more reliable and simpler solution would be to use raycasting client-sided. That way you wouldn’t even have to utilize RemoteEvents and the delay that comes with that.

ex:

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local function Is_At_Shop()
local ray = Ray.new(char.HumanoidRootPart.Position, Vector3.new(0, -1, 0) * 10)
local hit, p = workspace:FindPartOnRayWithIgnoreList(ray, {char})
local at_shop
if hit then
if hit.Name == "NAME OF THE SHOP PART!!" then
at_shop = true
end
end
shop_frame.Visible = at_shop --//Assuming all you want to do is toggle a frame's visibility
end
1 Like

You could either raycast and check the distance, or you could use Region3’s and check if the player is in the certain region or you could also make the field that has the .Touched and .TouchEnded a bit smaller than the “field” itself that way you’ll ensure that the player is actually inside the field.

1 Like

Region3 sounds nice. I’ll try that and see how it goes.