Please help me with making a bell toll in the noon, my code does nothing

I have written a script to make a bell toll and make sound whenever it is noon (between 11:30 and 12:30), but the code does nothing and I get no errors or prints. The code is as following:

local lighting = game:GetService("Lighting")
local clocktime = lighting.ClockTime
local toll = script.Parent.Sound

while task.wait(1) do
	if clocktime >= 11.5 and clocktime <= 12.5 and not toll.Playing then
		toll:Play()
		print("for whom the bell tolls, time marches on")
	end
end

I remember making a similar script before, and it had worked, but this one does not. I would like to know where I went wrong in this and how I could fix it. It is a loop which checks if the clocktime is past 11:30 (11.5) and before 12:30 (12.5), and it loops every second, although I had originally intended for it to loop every 10 seconds for the sake of optimization.

The game has a time of day cycle script that makes time move forward, which works as intended.

4 Likes

Does “for whom the bell tolls, time marches on” print to the console during 11:30 and 12:30? How are you testing clock time?

It does not print, though it should. It is how I know that the script is not working; it should print the phrase into the console when the conditions are met in the loop, but it does not, and neither does the bell sound play.

To test, I execute a server test in Studio and keep my eyes on the clocktime. I can see the numbers on Lighting.ClockTime change and I can see the position of the sun moving on the sky.

Does clock time go from 0.0 to 0.5 to 1 to 1.5 etc up to 24, in single-decimal increments? If clocktime, for example, goes like 0 to 500 to 1000 or 0 to 50 to 100 to 150? What increment does clock time change in

Edit: Is clocktime a numberValue or a intValue?

Rather than using increments, the time cycle script uses an equation so I can easily adjust how many real life minutes an ingame day is. As it is right now, 24 hours ingame pass in 10 real life minutes. As for ClockTime, it is a property of Lighting, and not a Value instance.

I assume your concern is if time is passing too fast for the script to catch it in the loop? I assure you that it is most likely not the case.

Ah I looked at ClockTime now, can you show me the code for your equation? I see now that it’s not a number value or int value

EDIT: I’ll be back in 10 mins or so

local dayLength = 10

local cycleTime = dayLength*60
local minutesInADay = 24*60

local lighting = game:GetService("Lighting")

local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime

local timeRatio = minutesInADay / cycleTime

if dayLength == 0 then
	dayLength = 1
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false

I must add that the code was not written by myself, but it seems to work well and efficiently.
And feel free to take your time. I will patiently await for your return.

Alright I’m going to create a new place to test it out gimme a little while

I have decided to take a different approach after looking for other ways to get time with loops.

local lighting = game:GetService("Lighting")
local clocktime = lighting.ClockTime
local toll = script.Parent.Sound

while task.wait(1) do
	if game.Lighting:GetMinutesAfterMidnight() > 12 * 60 and game.Lighting:GetMinutesAfterMidnight() < 13 * 60 and not toll.Playing then
		toll:Play()
		print("for whom the bell tolls, time marches on")
	end
end

This code works well. What do you think of it? I intend to make the loop every 10 seconds rather than 1 to reduce the load.

1 Like

Does it work? I’m still setting up in the place

This older one you showed does not work. It doesn’t do anything and gives no errors. The newer one does work, however.

WAIT I THINK I GOT IT
At the start of your script, you are indexing clocktime ONCE. You have to constantly index clocktime to keep up with the changing time. Something like this would work:

local lighting = game:GetService("Lighting")
local toll = script.Parent.Sound

while task.wait(1) do
    local clocktime = lighting.ClockTime
	if clocktime >= 11.5 and clocktime <= 12.5 and not toll.Playing then
		toll:Play()
		print("for whom the bell tolls, time marches on")
	end
end

Edit: I just realized you solved it!
Edit 2: If ur gonna use this, it does work

2 Likes

That is some great insight, and since you say it works, I will mark that as a solution for anyone else who sees this post. Thank you for assisting me with this code. I should remember to index things the right way.

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