Hello!
I want to make a bell system in my game where the bells ring every hour. How can I go about doing this?
Hello!
I want to make a bell system in my game where the bells ring every hour. How can I go about doing this?
Depends on what you mean with “hour”, if it’s an in-game hour I would either:
Run a script wit a wait()
while true do
wait(hour)
—do something
end
Or if you are checking the real life hour then you would do something like:
while true do
wait(3600)
—do something
end
If all you need to do is wait an hour every time it rings, then you only need a while loop. Like this:
while task.wait(3600) do --// 3600 seconds is one real life hour
bellSound:Play()
end
I would also recommend wrapping it in a coroutine
so it allows other code below it to run simultaneously:
coroutine.wrap(function()
while task.wait(3600) do
bellSound:Play()
end
end)()
And if you want it to play every hour, but stop repeating after a certain thing happens, then you can do this:
repeat
task.wait(3600)
bellSound:Play()
until #game:GetService("Players"):GetPlayers > 2 --// stops repeating when there is more than two players in the server.
You would do something like this:
while true do
if os.time()%3600 < 10 then
bellSound:Play()
task.wait(3540)
else
task.wait(5)
end
end
Written on DevForum. Code is unformatted.
This code will play the sound every real-life hour, with accuracy up to 5 seconds. On laggy experiences, increase the < 10
to a higher number.