Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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)
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? ahsan666666666666 264 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

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!

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! Icelogist 11 — 3y
0
You could also do #Game.Players:GetChildren() CocoDysphoria 67 — 3y
0
Too Late! Icelogist 11 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

On line 36 do

until Countdown <= 0 or NumPlayers.Value <= PRequired
0
I tried that and it didn't work I don' Icelogist 11 — 3y
0
try and instead of or, and is there any errors JustinWe12 723 — 3y

Answer this question