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

Is this a valid script?

Asked by 9 years ago

I have been trying to create a script where a brick sounds on the hour (GMT real time). Is there something I need to correct in here?

--MysticalWarrior

local time_zone = -5        -- Time zone, offset from GMT.

while true do
    wait (60*60)
    sound:Play()
end

local secondsInDay = 60*60*24

while true do
    local tick = math.fmod(tick(),secondsInDay)

    local hour = math.floor(tick/3600) + 5 + time_zone
    local minute = math.floor(tick/60 - 60*hour)
    local second = math.floor(math.fmod(tick,60))

    wait(1)
end


1 answer

Log in to vote
0
Answered by 9 years ago

Your trouble comes from your first while true do loop. Because it is an infinite loop, your script isn't executing any of the code after it. It is generally good practice to do any one-time setup in the script first, then put an infinite loop at the end to make sure the rest of the code runs first.

In your case, there are two options: make the first loop a coroutine, or put all the code in the loops into one master loop. I took the latter option in the second code block.

I explained with comments what wasn't working with your script in the first code block, then provide a more straightforward way in the second code block.

--MysticalWarrior

local time_zone = -5        -- Time zone, offset from GMT. This only cancels the +5 in the second loop. Just keep it simple and take both out.

while true do --this was your problem. It didn't allow any code after the loop to run at all.
    wait (60*60)
    sound:Play() --(I'm assuming "sound" is defined)
end

local secondsInDay = 60*60*24 --This is being used to find the number of seconds that have passed in the current day in the third line after this (the line that defines "tick")

while true do
    local tick = math.fmod(tick(),secondsInDay) --this finds the number of second that have passed in the current day

    local hour = math.floor(tick/3600) + 5 + time_zone --this find the hour of the current day, albeit in a somewhat roundabout way. The part after the closed parenthesis is completely unnecessary. 
    local minute = math.floor(tick/60 - 60*hour) --This finds the current minute of the current hour, again in a somewhat roundabout way.
    local second = math.floor(math.fmod(tick,60)) --this finds the current second of the current minute.

    wait(1)
end

Now the second block.

while wait(.2) do --this loop executes five times per second.
    if math.fmod(tick(), 3600) == 0 then --if it's on the hour, local time, then...
        --sound:Play() --play the sound
        print'sound' --(this is a debug print)
    end
    local ticker = tick() --find the number of seconds since the Unix epoch (1/1/1970 midnight)

    local hour = math.floor(math.fmod(ticker/3600, 24))--finds the current hour
    local minute = math.floor(math.fmod(ticker/60, 60))--finds the current minute
    local second = math.floor(math.fmod(ticker,60))--finds the current second
    print(hour, ':', minute, ':', second)--prints the time
end
Ad

Answer this question