I need help on triggering an event on a certain time

Hello there, I want to make a script that triggers something at a certain time of the day.

but for some reason, it didn’t work it just does not trigger

here the script:local Periode = 4


while wait() do
	game:GetService('Lighting').ClockTime = game:GetService('Lighting').ClockTime + 0.1
	local JOURSEMAINE = JOURS[JourDeCommencement]
	--	print('Test')
	--rint(CLASSE)
	--print(CLASSE[JourDeCommencement])


	if Periode == 4 then
		--	print('Test')
		if LIGHTING.ClockTime == 8 then
			print('period 1 commence dans 5 minute')
			if LIGHTING.ClockTime == 10 then
				print('Pediode 1 fini')
			end

		end
	end

end

I’m really tired right now so maybe that’s why my scripting isn’t the best but thx if y’all help

1 Like

This is the problem right here
image

Since you are checking if the clock time is 8, there is no way it will be 10 inside the if statement. I guess what you meant to do was this:

		--	print('Test')
		if LIGHTING.ClockTime == 8 then
			print('period 1 commence dans 5 minute')
		elseif LIGHTING.ClockTime == 10 then
			print('Pediode 1 fini')
		end

yea but it should still print the ‘period 1 commence dans 5 minute’ (also might go sleep soon so if i dont reply that’s why)

Also some recomendations:

  1. Avoid using while wait() do it is more common practice to do this instead:
while true do
	task.wait();
end;
  1. Don’t get the service every iteration. I see that you already have a variable named “LIGHTING” which I guess is the lighting service. Because of this I recommend replacing game:GetService("Lighting") with “LIGHTING”

  2. You can also do this instead since it makes it more readable:

- game:GetService('Lighting').ClockTime = game:GetService('Lighting').ClockTime + 0.1
+ LIGHTING.ClockTime += 0.1

Does ‘Test’ print?

Right here:
image

game.Lighting.LightingChanged:Connect(function(property)
	if game.Lighting.ClockTime == 8 then
		print("Ha!")
	end
end)

Try this. Replace the print with whatever you need it to do. Put it in a regular script.

2 Likes

This means that the ‘ClockTime’ property is never exactly 8.

if Lighting.ClockTime > 8 then
	--Do code.
end
1 Like

that’s what i was thinking but the problem with that is that i want something to trigger when it pass the time but i don’t want to trigger 300 time

if i remember correctly it did print

Then can you replace “Test” with LIGHTING.ClockTime so we can see what it is doing.

image
here’s what it does I think we could do math.floor to like round it?

If you mean math.floor to floor the clock time then it wouldn’t cut it since you are adding decimal values each time. These decimal values would make it so if you floor the value it will never increase since it will try to add a decimal value and be rounded.

However you can indeed use math.floor for the check:

		--	print('Test')
		if(math.floor(LIGHTING.ClockTime) == 8) then
			print('period 1 commence dans 5 minute')
		elseif(math.floor(LIGHTING.ClockTime == 10) then
			print('Pediode 1 fini')
		end

However, the above would make it so that it would fire multiple times each iteration. Because of this I want to ask you why are you adding decimal values in clock time? Instead of adding something like 0.01 or so.

well uh
image
i do that

I see, maybe you are just starting with an ambiguous value. Check if Lighting is starting with something like 0.392103 on workspace since that could also cause this problem.

image
well i don’t think i do

Can you add a print between the Lighting.ClockTime += 0.1 and while true do so that we can see the starting value of ClockTime?

image
the first number is 8 but somewhy it goes to like 8.1000004324304

I am seeing the problem, roblox is probably counting wrongly like all computers do. It is something related with the fact that binary can only use 2 digits. The problem is similar to something like us trying to represent 1/3 which would be 0.333333333333333…

This is what I mean:
image
image

The solution, now that you are sure there aren’t 2 counters going on at the same time is to use math.ceil like you suggested. In this case we are not using math.floor since we are counting up and not downwards.

Lighting.ClockTime = math.ceil(Lighting.ClockTime + 0.1);

this does work but it’s really fast and not really smooth (also sorry if it took long to reply)

1 Like

Yes sorry, I realized I was wrong this morning and forgot about it. I forgot to multiply/divide by 10 so it increases by .1 each time.

Lighting.ClockTime = math.ceil((Lighting.ClockTime + 0.1) * 10)/10;