Player debounce for Region3 detector

  1. What do you want to achieve?
    I would like to make a region3 player debounce. A region detection that will not run if a player has been detected twice. (They must leave the region and re-enter in order to sell their in-game items again)

  2. What is the issue?
    The script below checks for a player in the region, if it finds one, it sells all the critters the player has collected. It works fine, but the problem is, a player can sit in the region and spam “selling” which plays the sell sound effect a lot (constantly).

  3. What solutions have you tried so far?
    I have tried to add a “last player” variable, but it failed to provide any type of improvement from the original script.

Provided Script:

local sellPad = script.Parent
local regionPart = sellPad:WaitForChild("Region")

function getRegionFromPart(part)
	return - (part.Size/2), part.Position + (part.Size/2))

local sellRegion = getRegionFromPart(regionPart)

while wait() do
	local partsInRegion = workspace:FindPartsInRegion3(sellRegion)
	for i, v in pairs(partsInRegion) do
		local player = game.Players:GetPlayerFromCharacter(v.Parent)
		if player then
			--sell their critters
			local critters = player.playerDataFolder.CrittersCollected
			local critterWorth = player.playerDataFolder.CritterWorth
			local totalCritters = player.playerDataFolder.TotalCritters
			local cash = player.playerDataFolder.Cash
			if critterWorth and critters and cash and totalCritters then
				cash.Value += critters.Value * critterWorth.Value -- give them cash
				totalCritters.Value += critters.Value -- increase the total critters
				critters.Value = 0 -- reset the critters value

I don’t think a debounce is best here.

You have a very convenient TotalCritters value. Check to see if that’s greater than zero before playing the sell noise.

local totalCritters = player.playerDataFolder.TotalCritters
1 Like

That sounds great! I cannot believe I did not think of that. I have high hopes for this. :grinning_face_with_smiling_eyes:

1 Like