How to fix server uptime and longest server uptime script

Hi, there just be straight-forward I’m not a scripter. I need help fixing these scripts, I made some of them and asked ChatGPT for guidance. I’m not sure if ChatGPT messed up the scripts, but basically, the first one displays an uptime counter, and the second one displays the highest uptime ever (that’s what I’m having issues with).

Script 1 (Uptime)

local DataStoreService = game:GetService("DataStoreService")
local UptimeDataStore = DataStoreService:GetDataStore("UptimeDataStore")
local TextGui = script.Parent -- Assumes the script is placed inside the TextGui object
local startTime = tick() -- Get the start time of the game
local highestUptime = 0 -- Variable to store the highest uptime

-- Function to convert seconds to a formatted uptime string
local function formatUptime(seconds)
	local days = math.floor(seconds / (24 * 60 * 60))
	local hours = math.floor((seconds % (24 * 60 * 60)) / (60 * 60))
	local minutes = math.floor((seconds % (60 * 60)) / 60)
	local remainingSeconds = math.floor(seconds % 60)
	return string.format("%dd %dh %dm %ds", days, hours, minutes, remainingSeconds)
end

-- Function to update the TextGui's text
local function updateText()
	local uptime = tick() - startTime -- Calculate the uptime by subtracting the start time from the current time
	TextGui.Text = formatUptime(uptime) -- Update the TextGui with the current uptime

	-- Update the highest uptime if the current uptime surpasses it
	if uptime > highestUptime then
		highestUptime = uptime
	end
end

-- Save the highest uptime when the server closes
game:BindToClose(function()
	local success, error = pcall(function()
		UptimeDataStore:SetAsync("HighestUptime", highestUptime) -- Save the highest uptime value to the DataStore
	end)
	if not success then
		print("Failed to save highest uptime to DataStore:", error)
	end
end)

-- Update the text initially
updateText()

-- Update the text every second
while wait(1) do
	updateText()
end

Script 2 (Displays The Highest Time)

local DataStoreService = game:GetService("DataStoreService")
local UptimeDataStore = DataStoreService:GetDataStore("UptimeDataStore")
local TextGui = script.Parent -- Assumes the script is placed inside the TextGui object

-- Function to convert seconds to a formatted uptime string
local function formatUptime(seconds)
	local days = math.floor(seconds / (24 * 60 * 60))
	local hours = math.floor((seconds % (24 * 60 * 60)) / (60 * 60))
	local minutes = math.floor((seconds % (60 * 60)) / 60)
	local remainingSeconds = math.floor(seconds % 60)
	return string.format("%dd %dh %dm %ds", days, hours, minutes, remainingSeconds)
end

-- Retrieve the highest uptime from DataStore
local function getHighestUptime()
	local highestUptime = 0 -- Variable to store the highest uptime

	local success, result = pcall(function()
		local data = UptimeDataStore:GetSortedAsync(false, 1):GetCurrentPage() -- Retrieve the sorted data with the highest uptime
		if data and #data > 0 then
			highestUptime = data[1].value -- Set the highest uptime to the value of the first entry in the sorted data
		end
	end)

	if success then
		TextGui.Text = formatUptime(highestUptime)
	else
		print("Failed to retrieve highest uptime from DataStore:", result)
		TextGui.Text = "Error retrieving uptime data."
	end
end

-- Retrieve and display the highest uptime initially
getHighestUptime()

Thanks so much in advance, and sorry about asking.

1 Like