Script Not Getting Sunday At Midnight Right

Hello Devs,
Does anyone know why my script isnt getting the right time and always says “6 days and 59 hours”.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local MessagingService = game:GetService("MessagingService")
local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")

local FeedbackStore = DataStoreService:GetDataStore("GlobalFeedbackStore")

local MOD_USERNAMES = { ["BadKarmas_YT"] = true }
local FEEDBACK_CHANNEL = "GlobalFeedbackChannel"

local SubmitFeedbackEvent = ReplicatedStorage.Server.Remotes.FQABoard:WaitForChild("SubmitFeedback")
local SendReplyEvent = ReplicatedStorage.Server.Remotes.FQABoard:WaitForChild("SendReply")

local feedbackBoard = workspace.Map.Biboards.FQABillboard.Main.SurfaceGui.Holder.ScrollingFrame
local TimeLabel = feedbackBoard.Parent:WaitForChild("Time")
local template = script:WaitForChild("1Temp")

local FeedbackList = {}
local ResetTimestampKey = "FeedbackResetTimestamp"
local ResetTime

-- Safe DataStore setter with retry
local function safeSet(key, value)
	for i = 1, 3 do
		local ok = pcall(function()
			FeedbackStore:SetAsync(key, value)
		end)
		if ok then return true end
		task.wait(1)
	end
	warn("Failed to save key:", key)
	return false
end


local function getNextSundayMidnightUTC()
	local now = os.time(os.date("!*t"))
	local date = os.date("!*t", now)
	local daysUntilSunday = (7 - date.wday + 1) % 7
	if daysUntilSunday == 0 then
		daysUntilSunday = 7
	end
	local nextSundayTime = os.time({
		year = date.year,
		month = date.month,
		day = date.day + daysUntilSunday,
		hour = 0,
		min = 0,
		sec = 0
	})
	return nextSundayTime
end

local function loadFeedback()
	local success, data = pcall(function()
		return FeedbackStore:GetAsync("FeedbackList")
	end)
	if success and type(data) == "table" then
		FeedbackList = data
	end

	local success2, storedReset = pcall(function()
		return FeedbackStore:GetAsync(ResetTimestampKey)
	end)

	if success2 and storedReset and type(storedReset) == "number" and os.time(os.date("!*t")) < storedReset then
		ResetTime = storedReset
	else
		ResetTime = getNextSundayMidnightUTC()
		safeSet(ResetTimestampKey, ResetTime)
	end
end

loadFeedback()

local function saveFeedback()
	safeSet("FeedbackList", FeedbackList)
end

local function spawnFeedbackBoardEntry(feedback)
	if feedback.spawned then return end
	feedback.spawned = true

	local clone = template:Clone()
	clone.Question.Text = "Q. "..feedback.message
	clone.Answer.Text = feedback.reply and ("A. "..feedback.reply) or "A. (No reply yet)"

	local player = Players:FindFirstChild(feedback.playerName)
	if player then
		local thumbUrl = Players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size48x48)
		clone.UserID.Image = thumbUrl
	end

	clone.Parent = feedbackBoard
end

local function broadcastFeedback(feedback)
	FeedbackList[feedback.id] = feedback
	spawnFeedbackBoardEntry(feedback)
	saveFeedback()

	for _, player in pairs(Players:GetPlayers()) do
		SubmitFeedbackEvent:FireClient(player, feedback)
	end

	pcall(function()
		MessagingService:PublishAsync(FEEDBACK_CHANNEL, feedback)
	end)
end

MessagingService:SubscribeAsync(FEEDBACK_CHANNEL, function(message)
	local data = message.Data
	if data then
		FeedbackList[data.id] = data
		spawnFeedbackBoardEntry(data)
	end
end)

SubmitFeedbackEvent.OnServerEvent:Connect(function(player, text)
	local success, filteredText = pcall(function()
		return TextService:FilterStringAsync(text, player.UserId)
	end)
	if success then
		local finalText = filteredText:GetNonChatStringForBroadcastAsync()
		local id = HttpService:GenerateGUID(false)
		local feedback = { id = id, playerName = player.Name, message = finalText, reply = nil }
		broadcastFeedback(feedback)
	end
end)

SendReplyEvent.OnServerEvent:Connect(function(player, feedbackId, replyText)
	if not MOD_USERNAMES[player.Name] then return end
	if not FeedbackList[feedbackId] then return end

	local success, filteredText = pcall(function()
		return TextService:FilterStringAsync(replyText, player.UserId)
	end)
	if success then
		local finalText = filteredText:GetNonChatStringForBroadcastAsync()
		FeedbackList[feedbackId].reply = finalText
		broadcastFeedback(FeedbackList[feedbackId])
	end
end)

spawn(function()
	while true do
		local now = os.time(os.date("!*t"))

		if now >= ResetTime then
			FeedbackList = {}
			saveFeedback()
			ResetTime = getNextSundayMidnightUTC()
			safeSet(ResetTimestampKey, ResetTime)
		end

		local timeLeft = math.max(0, ResetTime - now)
		local days = math.floor(timeLeft / 86400)
		local hours = math.floor((timeLeft % 86400) / 3600)
		local minutes = math.floor((timeLeft % 3600) / 60)

		if days > 0 then
			TimeLabel.Text = string.format("Resets in %d days %d hours", days, hours)
		elseif hours > 0 then
			TimeLabel.Text = string.format("Resets in %d hours %d minutes", hours, minutes)
		else
			TimeLabel.Text = string.format("Resets in %d minutes", minutes)
		end

		task.wait(1)
	end
end)



for _, feedback in pairs(FeedbackList) do
	spawnFeedbackBoardEntry(feedback)
end

2 Likes

Hey there! I think the issue may be due to a misuse of os.date and os.time. Could you try this function?

local function getNextSundayMidnightUTC()
    local now = os.time()
    local date = os.date("*t", now)
    local daysUntilSunday = (7 - date.wday + 1) % 7
    if daysUntilSunday == 0 then
        daysUntilSunday = 7
    end
    local nextSundayTime = os.time({
        year = date.year,
        month = date.month,
        day = date.day + daysUntilSunday,
        hour = 0,
        min = 0,
        sec = 0
    })
    return nextSundayTime
end

Also, in your loop, could you replace the now variable with this:

local now = os.time()
2 Likes

I believe that works, It now says it will reset in “1 Day and 5 Hours”

Is that the correct output? Timezones, so I can’t tell :P.

2 Likes

I believe so, Im in the EST timezone

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