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

i made a timer in my game that runs forever, is there a way to make this shorter? it's just a timer

Asked by 5 years ago
Edited 5 years ago

it does work, it's just that the script seems to be too long to just be a timer

local hint = Instance.new('Hint',game.Workspace)
local seconds = game.Workspace.Value
while true do
    hint.Text = seconds.Value
    for i= 1,seconds.Value do
        wait(1)
        if seconds.Value == 1 then
            wait(1)
            seconds.Value = 0
            hint.Text = 0
            wait(1)
                hint.Text = 30
                wait(1)
                seconds.Value = 29
                hint.Text = seconds.Value
        else
            seconds.Value = seconds.Value -1
            hint.Text = seconds.Value
        end 
    end
end

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

You're right, it can be shorter. This is what I came up with. It also has a value it defaults to when it tries to make the value into a negative number, the var timeEndNum.

local hint = Instance.new('Hint',game.Workspace)
local seconds = game.Workspace.Value
local timeEndNum = 30

while wait(1) do -- This while loop will run every second.
    if not seconds.Value <= 0 then -- This checks to see if the value is not less than or equal to zero.
        seconds.Value = seconds.Value - 1 -- This subtracts one from the var 'seconds.'
        hint.Text = seconds.Value -- This tells the hint the number it should display.
    else
        seconds.Value = timeEndNum -- This switches the value to the var 'timeEndNum.'
        hint.Text = timeEndNum -- This switches the value to the var 'timeEndNum.'
    end
end

I think this is as compact as it can be. There may be better solutions, but this is what I came up with. Since it waits you don't need to put a wait in the while loop.

Ad

Answer this question