Make funciton run once in loop with debounce

while true do 
	local parts = workspace:FindPartsInRegion3(region, part)

	for i, part in pairs(parts) do
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		--local Char = workspace:FindFirstChild(player.Name)

		if player then 

			print(player.Name)
			-- Players timer for every single player in game
			spawn(function()
				if Debounce == true then return end

					for i,v in pairs(game.Workspace:GetChildren()) do
						if game.Players:FindFirstChild(v.Name) then

							local Players = game:GetService("Players")

							local plr = Players:FindFirstChild(v.Name)
							--if plr:FindFirstChild("InQueue") then
							StartTime()
							RE2:FireClient(plr)
							print("Remote Event Fired")
							--end
						end
					end
					
				Debounce = true

			end)
			
		end
	end

	wait(1)
end 

Goal: Here what I want to do then a player enters the region3 the spawn function runs only once. Then the while true do loop stops running and the spawn function runs only once

Problem: The Remote Event Fires 3 times before the debounce actually work.

1 Like

You didn’t set the debounce = true at the beginning. Should be

if Debounce == true then return end
Debounce = true
-- run stuff here
Debounce = false

It didnt run anything inside the script. I think it the while true do loop tbh.