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

How do I fix the "day" variable?

Asked by 6 years ago
Edited 6 years ago

I asked 2 questions about this already, then managed to figure out a mostly-complete date script. (Yes, IT WAS A PAIN! After I fix this, I'll put it into a guide so others don't have to go through my pain and suffering.)

_G.msg = Instance.new("Hint")
_G.msg.Parent = game.Workspace

while true do --yes, this may very well be the longest while true do loop ever created...
    wait(.1)
    _G.year = math.floor(os.time()/31557600) + 1970
    _G.leap = _G.year/4
    if math.floor(_G.leap) == _G.leap then
        _G.days = 366
    else
        _G.days = 365
    end

    _G.day = math.floor(math.floor(os.time()/86400) - (math.floor(os.time()/31557600) * 365.25))

    _G.msg.Text = (_G.day)

    _G.month = 0
    if _G.days == 365 then
        if (_G.day >= 1) and (_G.day <= 31) then _G.month = 1
        elseif (_G.day >= 32) and (_G.day <= 59) then _G.month = 2
        elseif (_G.day >= 60) and (_G.day <= 90) then _G.month = 3
        elseif (_G.day >= 91) and (_G.day <= 120) then _G.month = 4
        elseif (_G.day >= 121) and (_G.day <= 151) then _G.month = 5
        elseif (_G.day >= 152) and (_G.day <= 181) then _G.month = 6
        elseif (_G.day >= 182) and (_G.day <= 212) then _G.month = 7
        elseif (_G.day >= 213) and (_G.day <= 243) then _G.month = 8
        elseif (_G.day >= 244) and (_G.day <= 273) then _G.month = 9
        elseif (_G.day >= 274) and (_G.day <= 304) then _G.month = 10
        elseif (_G.day >= 305) and (_G.day <= 334) then _G.month = 11
        elseif (_G.day >= 335) and (_G.day <= 365) then _G.month = 12
        end
    elseif _G.days == 366 then
        if (_G.day >= 1) and (_G.day <= 31) then _G.month = 1
        elseif (_G.day >= 32) and (_G.day <= 60) then _G.month = 2
        elseif (_G.day >= 61) and (_G.day <= 91) then _G.month = 3
        elseif (_G.day >= 92) and (_G.day <= 121) then _G.month = 4
        elseif (_G.day >= 122) and (_G.day <= 152) then _G.month = 5
        elseif (_G.day >= 153) and (_G.day <= 182) then _G.month = 6
        elseif (_G.day >= 183) and (_G.day <= 213) then _G.month = 7
        elseif (_G.day >= 214) and (_G.day <= 244) then _G.month = 8
        elseif (_G.day >= 245) and (_G.day <= 274) then _G.month = 9
        elseif (_G.day >= 275) and (_G.day <= 305) then _G.month = 10
        elseif (_G.day >= 306) and (_G.day <= 335) then _G.month = 11
        elseif (_G.day >= 336) and (_G.day <= 366) then _G.month = 12
        end
    end

    _G.VDay = 1
    _G.BDay = 1
    _G.IDay = 1
    _G.HDay = 1
    _G.CDay = 1

    if _G.day == 45 then _G.VDay = 2
    elseif _G.days == 365 and _G.day == 167 then _G.BDay = 2
    elseif _G.days == 366 and _G.day == 168 then _G.BDay = 2
    elseif _G.days == 365 and _G.day == 185 then _G.IDay = 2
    elseif _G.days == 366 and _G.day == 186 then _G.IDay = 2
    elseif _G.days == 365 and _G.day == 304 then _G.HDay = 2
    elseif _G.days == 366 and _G.day == 305 then _G.HDay = 2    
    elseif _G.days == 365 and _G.day == 358 then _G.CDay = 2
    elseif _G.days == 365 and _G.day == 359 then _G.CDay = 2
    elseif _G.days == 366 and _G.day == 359 then _G.CDay = 2
    elseif _G.days == 366 and _G.day == 360 then _G.CDay = 2
    end
end

However, I have a problem. The value for _G.day is pretty accurate, but I tested it and it was off by one day. I need this to be accurate to the exact day. (By the way, this is trying to determine a value between 1 and 365 [or 366 if it's a leap year] to say what day of the year it is, NOT what day of the month it is.) So how can I fix this? Any help is appreciated. Thanks!

Also, everything is _G because I need them for other scripts. Remember those event ores from the last question? The _G.month and _G.day variables are what determines when they spawn.

0
it might be because you floored it. try math.ceil()ing it instead maybe. farizarps 132 — 6y
0
There is no need for day to be a global variable turtle2004 167 — 6y
0
just use local day = x turtle2004 167 — 6y
0
turtle2004, I need it to be a global variable because other scripts use them. JakesRevenge 6 — 6y
0
math.ceil()ing it seemed to work, so thanks. Leaving this open just in case a better solution comes along though. JakesRevenge 6 — 6y

Answer this question