Using OS.Time to create events that happen every few hours

Hi I want to try and create a script that will make events happen for a certain amount of time and have it do the event every few hours for each server at the same time. However I am still fairly new to this so I’ve had to use tutorials and this is the closest I’ve got but this isn’t quite what I want so any help to get this working would be appreciated

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

function convertToHMS(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) 
	local secondsOfTheDay = date["hour"]*3600 + date["min"]*60 + date["sec"]  
	local secondsLeftUntilMidnight = convertToHMS(86400 - secondsOfTheDay)

	if 86400 - secondsOfTheDay == 0 and game.ReplicatedStorage.BossActive.Value == false then
		game.ReplicatedStorage.BossActive.Value = true
		local Boss = script.Parent.Boss:Clone()
		Boss.Parent = workspace
		game.ReplicatedStorage.BossActive.Value = true
		game.Workspace.BossSpawnTimer.Numbers.SurfaceGui.TextLabel.Text = "BOSS ACTIVE"
	else
		game.Workspace.BossSpawnTimer.Numbers.SurfaceGui.TextLabel.Text = secondsLeftUntilMidnight
	end
end

What’s the problem with the code?

There are no errors I just want to adjust the code so it will activate the event every few hours. Along with giving it a certain amount of time before the event ends and the timer starts again. Apologies if its a very simple error to fix I’m still getting used to os.time so its not very clear to me on how I would make what I want work.

If I understand your code correctly your taking the current time in seconds and subtracting it from 86400 to check if its midnight?

Yes, thats correct so right now it will start the event every midnight.

I’m assuming the rest of your code is correct so this will only work if it is, but instead of subtracting take the % of the current time with the time you want it to run. For example if you want it to run every three hours, do “if secondsOfTheDay % 10800 == 0”. What this does is it divides the current time by three hours, if its been three hours the % operator returns 0.

You can replace 10800 with the time you want between events.

It seems to be working better now, However the Board that displays the timer is now going up rather than down.

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

function convertToHMS(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) 
	local secondsOfTheDay = date["hour"]*3600 + date["min"]*60 + date["sec"]  
	local secondsLeftUntilBoss = convertToHMS(secondsOfTheDay % 10800)

	if secondsOfTheDay % 10800 == 0 and game.ReplicatedStorage.BossActive.Value == false then
		game.ReplicatedStorage.BossActive.Value = true
		local Boss = script.Parent.Boss:Clone()
		Boss.Parent = workspace
		game.ReplicatedStorage.BossActive.Value = true
		game.Workspace.BossSpawnTimer.Numbers.SurfaceGui.TextLabel.Text = "BOSS ACTIVE"
	else
		game.Workspace.BossSpawnTimer.Numbers.SurfaceGui.TextLabel.Text = secondsLeftUntilBoss
	end
end
1 Like

local secondsLeftUntilBoss = convertToHMS(10800 - (secondsOfTheDay % 10800))

This fix should work if I did it correctly.

2 Likes

Perfect, this is working fine now thanks again! Very helpful