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

is it possible to update textlabel as time changes?

i tried using this script to decrease the time to the next day but it doesn't work.

local player = game.Players.LocalPlayer

local MTimePeriod = 60
local STimePeriod = 60

local tNums = {3600*24,os.time()} -- amount of seconds in a day and amount of seconds since epoch.
        local vLocal = (tNums[2]/tNums[1])-math.modf(tNums[2]/tNums[1]) -- Decimal point for day, 12PM = .5
        local vHours = math.floor(vLocal*24) -- The amount of hours into the day
local vMinutes = math.floor(vLocal*60*24)-(vHours*60) -- The amount of minutes into the hour
        local vSeconds = math.floor(vLocal*tNums[1])-(vHours*3600)-(vMinutes*60) -- The amount of seconds into the minute

    while true do
player.PlayerGui:WaitForChild("ScreenGui").Frame.Information.PersonalInfo.NextDay.NextDay.Text = MTimePeriod - vMinutes.. "m " ..STimePeriod - vSeconds.. "s"
wait()
end

2 comments

1

I updated & edited my answer.

0

Updated my chats.

1 answer

✓ Accepted answer

It looks like you're over-complicating things, and (possibly) using unnecessary chunks to get to that result:

A. I don't recommend using os.time, as it doesn't cover individual time-zones, and focuses on a particular one; I recommend using tick instead, as it'll cover individual time-zones, and is said to be more accurate.

B. Your code, as I've said b4, looks like you're over-complicating something simple, but you're used a lot of unnecessary measures.

C. You're code is messy & isn't tabbed correctly.

D. Your variables are confusing, and don't (necessarily) represent their purpose: what is tNums supposed to mean? What's it supposed to do? Same w/ vHours, vSeconds, vLocal, etc.

E. You're trying to put number values in along w/ string values, which could result in your code breaking; I recommend using tostring to counter this.

F. Your explanations aren't very clear, and what you're trying to do is very vague.

Now, onto the code, it's a jumble-fest of mathematical calculations that aren't necessary, at least from my point of view: you don't require that much code to get the hours, minutes, etc.

Let's start over:

-- Code

First off, instead of doing like you do, we're going to use a function to aid us in getting the time & returning it:

function CalculateAndReturnTime() -- Function "CalculateAndReturnTime" will be used to calculate & return the time values

end

Next, we'll set up a variable for the time:

function CalculateAndReturnTime()
	local CurrentTime = tick() -- tick is said to be more accurate than os.time, & it covers individual time-zones
end

Now that we have our time variable set up, we'll proceed to creating variables that represents their purpose, and the same w/ their names:

function CalculateAndReturnTime()
	local CurrentTime = tick() % 86400 -- To get 86400, you multiply Seconds by Hours by Day; 60 by 60 by 24; this will return the remainder of days (think of division, but having remainders: 5 / 26 = 5 R1)
	local CurrentHour = math.floor(CurrentTime / 3600) -- Calculates the remainder by the number of seconds in an hour, but keep it a whole number
	local CurrentMinute = math.floor(((CurrentTime / 3600) - CurrentHour) * 60) -- Same practices here, but we subtract the hour (w/ the decimal) by the hour (w/o the decimal), then multiply the result by 60 to get the minute
	local CurrentSecond = CurrentTime / 3600 -- Divide the current time by the number of seconds in an hour
end

Now that we have our mathematical calculations, we want the function to be able to return the results, which can be accomplished w/ the return function:

function CalculateAndReturnTime()
	local CurrentTime = tick() % 86400
	local CurrentHour = math.floor(CurrentTime / 3600)
	local CurrentMinute = math.floor(((CurrentTime / 3600) - CurrentHour) * 60)
	local CurrentSecond = CurrentTime / 3600
	
	return CurrentHour, CurrentMinute, CurrentSecond -- Will return the current hour, minutes, and seconds
end

But wait! If we were to use the code as is, then the time wont be correct, b/c it may look like 6:5:8! :O But this is a simple fix. :P We can use the if statement to check & see if the number is less than 10, and if so, apply a 0 to it, to get 06:05:08! (/O-O)/

function CalculateAndReturnTime()
	local CurrentTime = tick() % 86400
	local CurrentHour = math.floor(CurrentTime / 3600)
	local CurrentMinute = math.floor(((CurrentTime / 3600) - CurrentHour) * 60)\
	local CurrentSecond = CurrentTime / 3600
	
	if CurrentHour < 10 then -- If the current number is less than 10, then run the following chunk
		CurrentHour = "0" .. tostring(CurrentHour) -- tostring converts a number/ bool(?) into a string; will apply a zero to "CurrentHour"
	end
	if CurrentMinute < 10 then
		CurrentMinute = "0" .. tostring(CurrentMinute) -- Same here
	end
	if CurrentSecond < 10 then
		CurrentSecond = "0" .. tostring(CurrentSecond) -- Same here
	end
	
	return CurrentHour, CurrentMinute, CurrentSecond
end

Now, this is where I stop: I'm not going any further, as then I'll just be giving you the entire code, but this should be sufficient enough to help get you started. :)

Stuff touched on but didn't get into great detail

  1. Os.time

  2. Tick

  3. Tostring

  4. Functions

  5. Return

  6. If statements

  7. Time-Zones

Hope this helped you in any way. :)

EDIT

B4 I begin, I want to apologize for misunderstanding your answer, and assuming your code was messy. ;-;

Now, onto what you want accomplish: a countdown to zero to represent the time until the next day. (OH IT'S THE FINAL COUNTDOWN- DOO-DOO-DO-DO-DO-DOO :P)

We could take the current time, and have it be set on a basis, similar to my original code, but we'll make changes:

local SecondsInADay = 60 * 60 * 24 -- Will equal to 86400, the number of seconds in a day

Ok, now that we have the variable for how many seconds is in a day, we'll take my original code format, then have it set to be subtracted by how many seconds there're in a day:

function timeBeforeDayEnds()
	local SecondsInADay = 60 * 60 * 24
	local CurrentTime = tick() % 86400
	local CurrentHour = math.floor((CurrentTime / 60 / 60))
	local CurrentMinute = math.floor(((CurrentTime - CurrentHour) * 60))
	local CurrentSecond = (CurrentTime % 60) / 60 -- Same calculations as before
	
	local HoursBeforeDayEnds = ((SecondsInADay % 86400) / 60) - CurrentHour -- We subtract how many hours are in an actual day by the current hour
	local MinutesBeforeDayEnds = ((SecondsInADay % 86400) / 60 / 60) - CurrentMinute -- Same thing here, but w/ minutes
	local SecondsBeforeDayEnds = (((SecondsInADay % 86400) % 60) / 60) -- Same thing here, but w/ seconds
	
	return HoursBeforeDayEnds, MinutesBeforeDayEnds, SecondsBeforeDayEnds
end

Man, that was a lot of work tbh. /(ToT)\=3 I'm sorry, but I can't really get into detail about the subtraction & calculations; it's very, very complicated; but I'm 77% confident that when you use run code, it'll return the hours, minutes, and seconds before the day ends. :) If not, I apologize if it doesn't work, as I'm working off memory. ;-;

Now, I'm going to need some Tylenol, ugh, my brain... T-T

I hope this helped you in any way. :)

26 comments

0

i never said it was broken. it gives the time right. as in this picture https://i.gyazo.com/f0b4a34272c66e65530ab6824b96e0bd.png What im trying to do is make it countdown until it reached 00:00

0

Ah, ok. I think I see what you're trying to do now. :) I'll edit my answer.

0

and i'm using os.time because of this Warning: tick() returns local time, and time zones between servers may vary. A reliable option to get the same time on every server is to call os.time(). i need the same time on all servers

0

Ah that's okay; all you have to do (at least w/ my code) is replace tick w/ os.time. :P

0

I think I've got a headache now. ;-; #PrayForMyBrain

0

that makes it negative for some reason https://i.gyazo.com/dff240a98546b9db15816abfd0de293e.png, this is what it prints : -20 -4418623 0

0

you do know im just trying to change the textlabel text as time changes right?

0

...Doh! Ok, I'll try one more time. X'D

0

Wait, is it a LocalScript your using? And if so, is it in the StarterGui? Or is the problem that it's not updating the GUI?

0

Instead of using the LocalPlayer variable, then get to the TxtLbl from there, why not parent the script into the TxtLbl, and have the code change it's parent's text property? "script.Parent.Text = Code"

0

yea it's a LocalScript in starterGui

0

tried both Type of scripts inside the textlabel but didn't make the time go down, it only set it to 9m 46s and stayed there. i cant figure it out. I've done this before (while true do), not with time though.

0

repeat until doesnt work either -.-

0

Are you recieving any errors from the Output? And if so, can you tell me what it said? It may give us a clue as to what the problem is.

0

And is FE enabled? (Filtering Something-or-Other)

0

there's no output and FE isnt on

0

ok i added a print in the function to change the time and repeated it until newday == true the Print: (function: 1EA66E40 (x292)) it was print(time)... the print was after the script.Parent.Text but it still wasnt changing the text

0

nevermind. i got it working... skips a few seconds or 3 seconds sometimes

0

Yep, that's loops for you. :P I recommend setting it to something other than zero, like 1/ 44th of a second  or 1 second b4 going onto the next loop, for the wait function.

0

it was wait(1) xD

0

Really? lol

0

yea

0

30 (x2) 31 (x2) 33 (x4) 34 (x2) 36 (x2) 37 (x4) 38 (x2) 40 (x2) 41 (x3) printing the seconds while waiting 0.5 seconds

0

Yeah, w/ the wait function, it doesn't deliver time accurately; it yields the code around the specified time, but not exact. :/

0

oh well it's not exactly a problem :P

0

Ok. :P Glad to (attempt) to help you w/ your problem anyways. :) Have a good night. :D

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