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

Why the time go past 0?

Asked by
mine248 40
8 years ago

I made a complex script. But there was a problem. The timer went past 0. No errors on output.

Faulty script 1:

local replicated = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer

if replicated.Timer.Value == 0 and replicated.Intermission.Value == false then
    replicated.Timer.Value = 20
    replicated.Intermission.Value = true
elseif replicated.Timer.Value == 0 and replicated.Intermission.Value == true then
    replicated.Timer.Value = 3
    replicated.Intermission.Value = false
    replicated.Countdown.Value = true
elseif replicated.Timer.Value == 0 and replicated.Countdown.Value == true then
    replicated.Timer.Value = 121
    replicated.Intermission.Value = false
    replicated.Countdown.Value = false
end

while game.Players.NumPlayers <= 3 do
    if replicated.Timer.Value == 0 and replicated.Intermission.Value == true then
        local Blue = {}
        local Yellow = {}
        local Green = {}
        if player.leaderstats.Team == "Blue" then
            table.insert(Blue, player.Character)
        elseif player.leaderstats.Team == "Yellow" then
            table.insert(Yellow, player.Character)
        elseif player.leaderstats.Team == "Green" then
            table.insert(Green, player.Character)
        end
        local chosenplayer1 = Blue [math.random(1, #Blue)]:MoveTo(game.Workspace.R62_Trk1.Model.VehicleSeat)
        local chosenplayer2 = Yellow [math.random(1, #Yellow)]:MoveTo(game.Workspace.R62_Trk2.Model.VehicleSeat)
        local chosenplayer3 = Green [math.random(1, #Green)]:MoveTo(game.Workspace.R62_Trk3.Model.VehicleSeat)
        wait(120)
    end
end

Faulty script 2:

while true do
    repeat
        wait(1)
        replicated.Timer.Value = replicated.Timer.Value - 1
    wait() 
    until replicated.Timer.Value == 0 or game.Players.NumPlayers <= 3 and replicated.Timer.Value == 0
end

1 answer

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

In this script

while true do
    repeat
        wait(1)
        replicated.Timer.Value = replicated.Timer.Value - 1
    wait() 
    until replicated.Timer.Value == 0 or game.Players.NumPlayers <= 3 and replicated.Timer.Value == 0
end

replicated.Timer.Value is decremented repeatedly until it's 0. Then the loop runs again and it keeps going after 0 and doesn't stop.

In the first script, the while loop isn't just going to start looping again once the condition is true. The if statements aren't just going to do whatever it is they would have done if their conditions become true some time in the future. Use a Changed event on replicated.Timer to do things when the value changes.

Ad

Answer this question