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

Could someone help me with this script?

Asked by 8 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

This showed up with no errors in the output

game_ended = script.Parent.gameEnded
game_time = script.Parent.gameTime
game_time_test = script.Parent.gameTime_Test
--times = game_time.Value
times = game_time_test.Value
timesnum = times 
while game_ended.Value == false do
    if times== 0 then
    times = timesnum
    else script.Parent.Text = "Time Left : "..times
    wait(1)
    times = times - 1
    end
end
wait(game_time_test.Value)
--wait(game_time.Value)
game_ended.Value = true
script.Disabled = true

This is supposed to countdown by game_time_test's Value (10), and disable after it's done. Any help please?

0
What does it do instead? BlueTaslem 18071 — 8y

1 answer

Log in to vote
0
Answered by
funyun 958 Moderation Voter
8 years ago

I've reformatted the script a bit to understand it better. It should still work precisely like yours.

--game_time = script.Parent.gameTime
--times = game_time.Value

game_ended = script.Parent.gameEnded
game_time_test = script.Parent.gameTime_Test
times = game_time_test.Value --10
timesnum = times --Also 10

while game_ended.Value == false do
    if times <= 0 then
        times = timesnum --Reset times to 10
    else
        script.Parent.Text = "Time Left : "..times
        wait(1)
        times = times - 1
    end
end

wait(game_time_test.Value) --Wait 10 seconds
--wait(game_time.Value)
game_ended.Value = true --Try to turn off the loop
script.Disabled = true

The problem here is that loops will not let you continue on in the script until they're done. So, you can't turn the loop off like that. To fix this, we have the built-in spawn function to let loops run in the background. Example:

spawn(function()
    while wait(1) do
        print("hey")
    end
end)

spawn(function()
    while wait(.8) do
        print("hi")
    end
end)

while wait(.7) do
    print("what's up")
end

To apply this to your script:

--game_time = script.Parent.gameTime
--times = game_time.Value

game_ended = script.Parent.gameEnded
game_time_test = script.Parent.gameTime_Test
times = game_time_test.Value --10
timesnum = times --Also 10

spawn(function() --Spawning a thread so it runs in the background.
    while game_ended.Value == false do
        if times <= 0 then
            times = timesnum --Reset times to 10
        else
            script.Parent.Text = "Time Left : "..times
            wait(1)
            times = times - 1
        end
    end
end)

wait(game_time_test.Value) --Wait 10 seconds
--wait(game_time.Value)
game_ended.Value = true
script.Disabled = true
0
Thx! PyccknnXakep 1225 — 8y
Ad

Answer this question