Midnight detection script isn't working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    i’m making a gacha system for my game, and i want to reset the featured item when it reaches midnight.

  2. What is the issue? it doesn’t really work. it detects midnight, but it doesn’t roll the number.
    ad there is no error warnings when i look at the output.

  3. What solutions have you tried so far? i have looked at a lot of other topics and tutorial, but didn’t find anything particularly useful.

my code :

function Format(Int)
	return string.format("%02i", Int)
end

function convertToHMS(Seconds) -- Converts seconds into hours, minutes, and seconds
	local Minutes = (Seconds - Seconds%60)/60
	Seconds = Seconds - Minutes*60
	local Hours = (Minutes - Minutes%60)/60
	Minutes = Minutes - Hours*60
	return Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)
end

while wait(0.5) do
	local Time = os.time()
	local date = os.date("!*t", Time) --Gets the Universal Standard time as a table
	local secondsOfTheDay = date["hour"]*3600 + date["min"]*60 + date["sec"] -- Finds how many seconds have passed in the day

	local secondsLeftUntilMidnight = convertToHMS(86400 - secondsOfTheDay) -- There are 86400 seconds in a day, so this finds how much time is left.

	print(secondsLeftUntilMidnight)

	if 86400 - secondsOfTheDay == 0 then -- If there are 0 seconds left of the day then do stuff
		
		local storage = game:GetService("ReplicatedStorage")
		local id = storage.Values:WaitForChild("FeaturedID")
		
		local chance = math.random(1, 11)
		
		id.Value = chance
		
	end
end

well if you’re looking for it to be exactly zero bad news it could go lower or higher and just barely miss it. That could be whats happening

what part should i change then?

the part I quoted needs to be changed

alright ill try to change the code

1 Like

how about this? this should detect 5 seconds before midnight right?

	if 86400 - secondsOfTheDay < 5 then

Yes it should. Tell me if it works!

ok, need to wait till tomorrow tho

1 Like