How to make a event run only once in a loop

So basically what i want to do is to make a event run only once in while loop i tried to do this but its doesnt seems to work:

local Zone = require(game.ReplicatedStorage.Zone)
local container = game.Workspace.Container
local zone2 = Zone.new(container)
local Event = game.ReplicatedStorage.Events.RemoteEvent
local gs = workspace.gamestatus

while true do
	local playersArray = zone2:getPlayers()
	if #playersArray == 1 then
		gs.Value = 1
		if gs.Value == 1 then
			Event:FireServer()
			gs.Value = 0
			print(gs.Value)
		else
			print("!!!!!!!!")
		end
	end
	wait(0.1)
end
1 Like

maybe try using debounces ?

not sure, but here’s a similar devforum post that i found while searching for a solution-

Use a debounce like this one here:

local Zone = require(game.ReplicatedStorage.Zone)
local container = game.Workspace.Container
local zone2 = Zone.new(container)
local Event = game.ReplicatedStorage.Events.RemoteEvent
local gs = workspace.gamestatus
-- Debounce set to false here
local scriptHasRun = false

while true do
	local playersArray = zone2:getPlayers()
	if #playersArray == 1 and scriptHasRun == false then
		gs.Value = 1
		if gs.Value == 1 then
			Event:FireServer()
			gs.Value = 0
			scriptHasRun = true
			print(gs.Value)
		else
			print("!!!!!!!!")
		end
	end
	wait(0.1)
end

1 Like

Why are you using a loop if you want something to happen once? If there’s some reason why, would breaking the loop when you FireServer fix the issue?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.