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

If statement with While loop isn't working in Local Script?

Asked by 6 years ago

Hi, i've a Local Script that controls time display and gui animation when there is extra added time or the game ends. Anyway the time script is controlled by server script ( to display the same time for everyone ). The problem is that the local script isn't doing the while true do part.

Local Script:

01local added = game.Workspace.Time.Added.Value
02local waitsecs = game.Workspace.Time.Wait.Value
03local min = game.Workspace.Time.Minutes.Value
04local sec = game.Workspace.Time.Seconds.Value
05local randomsec = game.Workspace.Time.Wait.Value
06 
07game.Workspace.Time.Changed:Connect(function()
08    script.Parent.Purple.Time.Text.Text = game.Workspace.Time.Value
09end)
10 
11while true do
12    wait(waitsecs)
13    print("Check") -- Checking if the main While loop is working
14    if min == 45 and sec >= 10 then
15        print("Extra") -- Checking if the If statement  is running or not but it isn't
View all 28 lines...

1 answer

Log in to vote
1
Answered by
cailir 284 Moderation Voter
6 years ago

Hello, What is happening is that the if is checking if min(game.Workspace.Time.Minutes.Value) is equals to 45 and sec(game.Workspace.Time.Seconds.Value) is equals or more than 10, but the value will never change, cause it will only get it on start.

Fixed code:

01local randomsec = game.Workspace.Time.Wait.Value -- 'randomsec' and 'waitsecs' are the same values, so no need to 2 variables
02 
03game.Workspace.Time.Changed:Connect(function()
04    script.Parent.Purple.Time.Text.Text = game.Workspace.Time.Value
05end)
06 
07while true do
08    local added = game.Workspace.Time.Added.Value
09    local min = game.Workspace.Time.Minutes.Value
10    local sec = game.Workspace.Time.Seconds.Value
11    wait(waitsecs)
12    print("Check") -- Checking if the main While loop is working
13    if min == 45 and sec >= 10 then
14        print("Extra") -- Checking if the If statement  is running or not but it isn't
15        script.Parent.AddedTime.Disabled = false
View all 27 lines...
0
Well that works, i forgot that the min's won't update. As well as randomsec and waitsecs aren't the same values i just defined them wrong. Thanks again vBaRaAx02 1 — 6y
1
It's better to use 'while wait() do' instead of 'while true do' or roblox will kill your script cailir 284 — 6y
Ad

Answer this question