[SOLVED] Repeating error occurs in my script. I need help!

Hello,

I need help with this specific script. The goal I’m trying to achieve here is that at the specified time, an event occurs/ends. So in this script, there are 3 events that I want to happen. But for some reason, once a specific time hits, in this case, 7:00 AM (Game time), it prints out the following in the server output in an alternating fashion and it keeps repeating and does not end:

false Event has ended at 8.5:00
true Event has ended at 7:00

How can this issue be resolved?

local Event1 = false
local Event2 = false
local Event3 = false
local targetTime = {
	[1] = 7,
	[2] = 8.5,
	[3] = 9,
	[4] = 10.5,
	[5] = 11,
	[6] = 12.5
}

local function handleEventStatus(EventType, startTime, endTime)
	if startTime then
		EventType = true
		print(tostring(EventType) .. " Event has started at " .. startTime .. ":00")
	end
	if endTime then
		EventType = false
		print(tostring(EventType) .. " Event has ended at " .. endTime .. ":00")
	end
end

local function checkEventTime(currentHour)
	if currentHour == targetTime[1] then
		Event1 = handleEventStatus(Event1, targetTime[1], targetTime[2])
	end
	if currentHour == targetTime[3] then
		Event2 = handleEventStatus(Event2, targetTime[3], targetTime[4])
	end
	if currentHour == targetTime[5] then
		Event2 = handleEventStatus(Event2, targetTime[5], targetTime[6])
	end
end

game:GetService("RunService").Heartbeat:Connect(function()
	local currentTime = game.Lighting.ClockTime
	local currentHour = math.floor(currentTime)
	checkEventTime(currentHour)
end)
2 Likes

I suggest changing that part of the code. Look at my idea how to fix the issue:

game:GetService("Lighting"):GetPropertyChangedSignal("ClockTime"):Connect(function()
	local currentTime = game.Lighting.ClockTime
	local currentHour = math.floor(currentTime)
	checkEventTime(currentHour)
end)
2 Likes

This helped me a little bit. But I ended up fixing this part to prevent the constant repeating pattern:

local function handleEventStatus(EventType, startTime, endTime)
	if startTime then
		if EventType == false then
		EventType = true
		print(tostring(EventType) .. " Event has started at " .. startTime .. ":00")
		end
	end
	if endTime then
		if EventType == true then
		EventType = false
		print(tostring(EventType) .. " Event has ended at " .. endTime .. ":00")
		end
	end
end

But now the issue is that this prints at the same time when it’s at 7 AM (Game Clock Time):

false Event has ended at 8.5:00
true Event has ended at 7:00

I am trying to do it as follows: when the clock time reaches 7:00 AM the Event starts and once it’s 8.5 (8:30 Clock Time), then the event ends only at that time.

1 Like

I think that I found something in the code to fix. Look at the variable declarations:

local Event1 = false
local Event2 = false
local Event3 = false

We have three event booleans. Look at your function and at my comment added to it:

local function checkEventTime(currentHour)
	if currentHour == targetTime[1] then
		Event1 = handleEventStatus(Event1, targetTime[1], targetTime[2])
	end
	if currentHour == targetTime[3] then
		Event2 = handleEventStatus(Event2, targetTime[3], targetTime[4])
	end
	if currentHour == targetTime[5] then
		-- Assigning a value to Event2. I think that you meant assigning a value to Event3
		Event2 = handleEventStatus(Event2, targetTime[5], targetTime[6])
	end
end

The fix here is replacing the mentioned Event2 with Event3

I noticed that too. But even when I changed it, the same issue still occurs.

game:GetService("Lighting"):GetPropertyChangedSignal("ClockTime"):Connect(function()
	local currentTime = game.Lighting.ClockTime
	local currentHour = math.floor(currentTime)
	checkEventTime(currentHour)
end)

In this case the event fires and sets currentHour to the same number multiple times. For example numbers 7, 7.2, 7.3 and 7.7 after rounding down are 7. The script passes the same number for checkEventTime function multiple times making it so the same print statements display the same thing.

I think I see what you mean, which it doesn’t really occur nor do I have an issue with that. But the thing is, even if it’s still at 7:00 AM (targetTime[1]), it fires targetTime[2] at the same time, even though it’s not 8.5 AM yet in clock time. That’s the problem that I am having trouble issue with. The script doesn’t detect when to end the event but fires it when its not suppose to fire yet.

I think that it solves the problem but let me know more if I am wrong.

local function handleEventStatus(EventType, startTime, endTime)
	if startTime then
		EventType = true
		print(tostring(EventType) .. " Event has started at " .. startTime .. ":00")
		task.spawn(function()
			while lighting.ClockTime < endTime do
				lighting:GetPropertyChangedSignal("ClockTime"):Wait()
			end
			print(tostring(EventType) .. " Event has ended at " .. endTime .. ":00")
		end)
	end
	--if endTime then
	--	EventType = false
	--	print(tostring(EventType) .. " Event has ended at " .. endTime .. ":00")
	--end
end

This script works but I had to fix some issues a little bit and prevent the repeating output to print multiple times again. Also, it was missing “game” before “lighting.ClockTime”. But overall this works well! Thank you so much for your help. I really appreciate it!

My bad. I declared lighting somewhere but I forgot to mention that. Good thing that you figured this out. Good :smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.