Help With Stopping Function

Hi, devs! I am back at it again working on my project. I wanted to create a timer system that repeats itself and it worked! I wanted to make it stop if more than enough people have joined the room which is done by a proximity prompt but it repeats the function twice. Any help would be greatly appreciated!

local function timer()

	for i = IntermissionLength, 0, -1 do
		if space < 10 then
	        InSession.Value = false
		    wait(1)
		    Status.Value = "Room 1 Intermission: ".. i .." seconds left to enter!"
			print(InSession.Value)
		else
			for i = InSessionLength, 0, -1 do
				InSession.Value = true
				wait(1)
				Status.Value = "Room 1 Watching "..chosenShortName
				print(InSession.Value)
			end
		end
	end
	for i = InSessionLength, 0, -1 do
		if space < 10 then
		    InSession.Value = true
		    wait(1)
		    Status.Value = "Room 1 Watching "..chosenShortName
			print(InSession.Value)
		else
			break
		end
	end
end

The Players:GetPlayers() method of the Players Service can be utilized in conjunction with the length operator → # to check how many players are in the game at any given moment.

The value that is returned from #Players:GetPlayers() could be compared to a value (such as the minimum amount of players that need to join to start the game early) in an “if then” conditional statement within the loops to make sure that the timer stops if enough players have joined the game already. Here’s an example:

-- Top of the script
local Players = game:GetService("Players")
local requiredPlayerCount = -- Insert a number that will represet the minimum amount of players required to start

----------

--[[
Inside of the loops, introduce a condition that will break the loop early if the
value returned from the :GetPlayers() method exceeds the number
that was assigned to the "requiredPlayerCount" variable
--]]

if #Players:GetPlayers() >= requiredPlayerCount then
    break
end

Additional Resources

Operators Article

Conditional Statements Article

Oh, sorry for my wording. I meant when a player joined a room which is done by a proximity prompt.

In that case, you could have a variable that keeps count of how many players have activated that ProximityPrompt / are in the room. Here’s an example:

local numberOfPlayersInTheRoom = 0

ProximityPrompt.TriggerEnded:Connect(function(player)

--[[
If it's possible for the player to activate the ProximityPrompt without being
teleported to the room, make sure that the "numberOfPlayersInTheRoom" variable
is not updated immediately


Once you have confirmed that the player is allowed to enter
and that they have been teleported, then the number assigned to the variable
can be increased by a value of 1
--]] 

    numberOfPlayersInTheRoom += 1
end)

--[[
Before the next intermission begins, I'd suggest resetting the 
"numberOfPlayersInTheRoom" variable to 0 so that the timer
will not end abruptly because the variable would be referring to the
amount of players that were in the room for the previous round/match
--]]

Because the activation of that ProximityPrompt could indicate that the player has entered the room, the “numberOfPlayersInTheRoom” variable could be increased by a value of one to reflect the increase in player count.

Afterward, the explanation I provided in my original reply to this topic could be utilized with this new variable, instead:

-- Top of the script
local requiredPlayerCount = -- Insert a number that will represet the minimum amount of players required to start
local numberOfPlayersInTheRoom = 0

----------

--[[
Inside of each loop, introduce a condition that will break the loop early if the
value returned from the "numberOfPlayersInTheRoom" variable exceeds the number
that was assigned to the "requiredPlayerCount" variable
--]]

if numberOfPlayersInTheRoom >= requiredPlayerCount then
    break
end
1 Like