Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you pause a timer when the Number of players is under 2?

I am making it so that if a player leaves while the timer is counting down it would stop. but it would just reset the timer and play it over and over again. I can't figure out a solution to this problem.

ReplicatedStorage = game:GetService("ReplicatedStorage")
Status = ReplicatedStorage:WaitForChild('Status')
Timer = ReplicatedStorage:WaitForChild('Timer')
PRequired = 2
NumPlayers = ReplicatedStorage.Players

if NumPlayers.Value >= PRequired then
	print("Players Ready!")
else
	Status.Value = 'Must have 2 or more players to play'
	Timer.Value = '...'
	repeat wait(1) until NumPlayers.Value >= PRequired
	print("Players Ready!")
end

--Intermission

local Countdown = 20 -- intermission	
print("Countdown incoming!")

repeat wait(1)
	Countdown = Countdown - 1

	Status.Value = 'Intermission'
	Timer.Value = Countdown
	if NumPlayers.Value <= PRequired then
		Status.Value = 'Timer Paused must have 2 or more players to play.'
		Timer.Value = '...'
		print("Timer paused")
		repeat wait() until NumPlayers.Value >= PRequired
		Countdown = 20
		Status.Value = 'Intermission'
		Timer.Value = Countdown
		print("Continued")
	end
until Countdown <= 0

PlayerCheck Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerCount = ReplicatedStorage.Players

game.Players.PlayerAdded:Connect(function()
	PlayerCount.Value = PlayerCount.Value + 1
	print(PlayerCount.Value)
end)

game.Players.PlayerRemoving:Connect(function()
	PlayerCount.Value = PlayerCount.Value -1
	print(PlayerCount.Value)
end)

1 comment

0

by repeat do you mean it resets to 20 and when it reaches 0 it goes back up to 20 and keeps doing that?

2 answers

✓ Accepted answer

Well, im not good at scripting but you could try this.

local PlayerCount = 0
local Players = game:GetService("Players")
local TimerScript = game.Workspace.Script --your timer script

Players.PlayerAdded:Connect(function(player)
      PlayerCount = PlayerCount + 1
end)

Players.PlayerRemoving:Connect(function(player)
      PlayerCount = PlayerCount - 1
end)

if PlayerCount == 1 then
      TimerScript.Disabled = true
else
      TimerScript.Disabled = false
end

please tell me if my script wont work, as im bad at scripting, hope this helps!

3 comments

0

This is a really weird way to solve this but I will accept it anyway because it gave me an idea about how to do it. Thanks for the inspiration!

0

You could also do #Game.Players:GetChildren()

0

Too Late!

On line 36 do

until Countdown <= 0 or NumPlayers.Value <= PRequired

2 comments

0

I tried that and it didn't work I don'

0

try and instead of or, and is there any errors

This discussion is preserved as it was. New answers, comments, and votes are closed.