So, i’m currently trying to make a time and date script for game, but the date script isn’t working. It doesn’t change at all though, I was wondering if I could get some help with it.
Heres the script:
local Days = {
-- The days of the week would go inside of this table as strings
}
local text = script.Parent
while true do
wait(1)
for i = 1, #Days do
local CurrentDay = Days[i]
text.Text = CurrentDay
repeat wait(1) until game.Lighting.ClockTime == 24
-- I want the loop to run again when the time hits 24 or 12 AM
end
end
local Days = {"Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"}
local data = os.date("%a")
local text = script.Parent
while true do
wait(1)
text.Text = data
end
local function formatDay(day: number)
local days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
return days[day % 7 ~= 0 and day % 7 or 7]
end
print(formatDay(14)) --> Sunday
What I would do is check whenever the time changes and see if it has passed midnight, then change the day. I also would do that on the server and utilize a StringValue in ReplicatedStorage so clients can read off of that. Here is what this code would look like:
-- Services --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")
-- Variables --
local CurrentDay = 1
local DayValue = ReplicatedStorage:WaitForChild("CurrentDay")
local LastTime = Lighting.ClockTime
-- Tables --
local Days = {"Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"}
-- Scripting --
Lighting.Changed:Connect(function()
local CurrentTime = Lighting.ClockTime
if LastTime > CurrentTime then
CurrentDay += 1
if CurrentDay > 7 then
CurrentDay = 1
end
DayValue.Value = Days[CurrentDay]
end
LastTime = CurrentTime
end)