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

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

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:

local added = game.Workspace.Time.Added.Value
local waitsecs = game.Workspace.Time.Wait.Value
local min = game.Workspace.Time.Minutes.Value
local sec = game.Workspace.Time.Seconds.Value
local randomsec = game.Workspace.Time.Wait.Value

game.Workspace.Time.Changed:Connect(function()
	script.Parent.Purple.Time.Text.Text = game.Workspace.Time.Value
end)

while true do
	wait(waitsecs)
	print("Check") -- Checking if the main While loop is working
	if min == 45 and sec >= 10 then
		print("Extra") -- Checking if the If statement  is running or not but it isn't
		script.Parent.AddedTime.Disabled = false
		script.Parent.Purple.Time.Added.AddedT.Text = "+" .. added .. "'"
		script.Parent.Purple.AddedTimeValue.Text = "+" .. added .. "'"
	end
		if min == 45 + added and sec >= randomsec then
		print("End")
		wait(2)
		script.Parent.OutAnimation.Disabled = false
		script.Parent.InAnimation.Disabled = true
		wait(.8)
		break
	end
end	

1 answer

✓ Accepted answer

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:

local randomsec = game.Workspace.Time.Wait.Value -- 'randomsec' and 'waitsecs' are the same values, so no need to 2 variables

game.Workspace.Time.Changed:Connect(function()
    script.Parent.Purple.Time.Text.Text = game.Workspace.Time.Value
end)

while true do
    local added = game.Workspace.Time.Added.Value
    local min = game.Workspace.Time.Minutes.Value
    local sec = game.Workspace.Time.Seconds.Value
    wait(waitsecs)
    print("Check") -- Checking if the main While loop is working
    if min == 45 and sec >= 10 then
        print("Extra") -- Checking if the If statement  is running or not but it isn't
        script.Parent.AddedTime.Disabled = false
        script.Parent.Purple.Time.Added.AddedT.Text = "+" .. added .. "'"
        script.Parent.Purple.AddedTimeValue.Text = "+" .. added .. "'"
    end
        if min == 45 + added and sec >= randomsec then
        print("End")
        wait(2)
        script.Parent.OutAnimation.Disabled = false
        script.Parent.InAnimation.Disabled = true
        wait(.8)
        break
    end
end 

2 comments

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

1

It's better to use 'while wait() do' instead of 'while true do' or roblox will kill your script

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