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

How could I make my time formatting script more efficient?

Hello. I've been working on a timer recently and I want to make it very easy to read, so what i've done is made code that converts day, hour, minute variables etc into an easy to read time format for example variables are day: 2 hour: 1 minute: 0 second: 16 it would return "2 Days, 1 Hour, 16 Seconds"

or day: 1 hour: 0 minute: 5 second: 4 would return "1 Day, 5 Minutes, 4 Seconds"

Here is the code: https://pastebin.com/raw/M4WwSZHE

as you can tell the code is WAY TOO LONG. WAY too long. I have done a lot of searching and I cant find anything to help make this MESS any shorter I would really appreciate it if you could tell me a way to make this shorter

Thanks!

1 comment

0

What do you mean?

1 answer

string.format("%d Hours, %d Minutes, %d Seconds", hr, min, sec)

The format function, formats a string. In string patterns, %d is a digit character.

In this case, you can do something like this

local function format(hr, min, sec)
	return string.format("%d Hours, %d Minutes, %d Seconds", hr, min, sec);
end

print(format(12, 24, 36)); --> 12 Hours, 24 Minutes, 36 Seconds

You can add checks so if the number is 1 you can make it say 1 Hour (for example) rather than 1 Hours.

2 comments

0

Yes, but if I add the checks then it will end like the pastebin script I made above

0

That's totally your problem.

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