#ive made this and tried to make it work using chat gpt but it doesnt work here are the scripts
1 messages · Page 1 of 1 (latest)
local DataStoreService = game:GetService("DataStoreService")
local TimePlayedStore = DataStoreService:GetDataStore("TimePlayed")
-- Table to track session time
local sessionTime = {}
-- Save time when player leaves
local function saveTime(player)
local userId = player.UserId
local playedTime = sessionTime[userId] or 0
-- Save to DataStore
local success, err = pcall(function()
local previous = TimePlayedStore:GetAsync(userId) or 0
TimePlayedStore:SetAsync(userId, previous + playedTime)
end)
if not success then
warn("Error saving time for", player.Name, ":", err)
end
end
-- Track time
Players.PlayerAdded:Connect(function(player)
local userId = player.UserId
sessionTime[userId] = 0
while player and player.Parent do
wait(1)
sessionTime[userId] += 1
end
end)
Players.PlayerRemoving:Connect(function(player)
saveTime(player)
end)
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
saveTime(player)
end
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SurfaceGui = script.Parent -- Adjust if needed
local TimePlayedStore = game:GetService("DataStoreService"):GetDataStore("TimePlayed")
local function formatTime(seconds)
local mins = math.floor(seconds / 60)
local hrs = math.floor(mins / 60)
mins = mins % 60
return string.format("%02d:%02d", hrs, mins)
end
local function updateLeaderboard()
local leaderboard = {}
for _, player in ipairs(Players:GetPlayers()) do
local userId = player.UserId
local success, totalTime = pcall(function()
return TimePlayedStore:GetAsync(userId)
end)
if success and totalTime then
table.insert(leaderboard, {name = player.Name, time = totalTime})
end
end
table.sort(leaderboard, function(a, b)
return a.time > b.time
end)
-- Clear and update GUI
local frame = SurfaceGui:FindFirstChild("LeaderboardFrame")
if not frame then return end
for _, child in pairs(frame:GetChildren()) do
if child:IsA("TextLabel") then
child:Destroy()
end
end
for i, entry in ipairs(leaderboard) do
local label = Instance.new("TextLabel")
label.Size = UDim2.new(1, 0, 0, 30)
label.Position = UDim2.new(0, 0, 0, (i - 1) * 30)
label.Text = string.format("%d. %s - %s", i, entry.name, formatTime(entry.time))
label.TextScaled = true
label.BackgroundTransparency = 1
label.TextColor3 = Color3.new(1, 1, 1)
label.Parent = frame
end
end
while true do
updateLeaderboard()
wait(10)
end
any errors?
well whats not working abt it
amir do you know much programming or are you 100% reliant on chatgpt?
nah same here dont worry brother
i swear some of these programmers make me feel like i have like maybe an iq of 2 or 3
loll
heres a tip look for a tutorial on how to make a global leaderboard - get that working first
then find a tutorial on how to track time played
alrr
then attach it to the leaderboard
here hold on i found a good tutorial for leaderboards
alr send
looking for it
yeah i cant find the video i even searched my history in youtube - it was easier to understand that most of the videos out there - anyways im sure as long as you copy exactly what most videos say it will work - you should be able to figure out where it gets the values from something like 'player.leaderstats.coins.Value' and just replace it with player.timeplayed.time.Value or something like that
alr
Yo is your script inside surface gui