I have put time spent system so when someone spends time in the game it counts on the leaderboard,
It’s working and everything is good! but When the player leaves it doesn’t get saved and that annoys me.
I tried a lot of solutions and didn’t work…
here is the script
local function addTime(player)
while true do
wait(1)
player.Seconds.Value = player.Seconds.Value + 1
if player.Seconds.Value == 60 then
player.Seconds.Value = 0
player.leaderstats.Minutes.Value = player.leaderstats.Minutes.Value + 1
end
end
end
You’ll need to utilize Data Stores in order to read/write player data, this is actually really simple to set up since you have the infrastructure done already:
local ds = game:GetService("DataStoreService"):GetDataStore("Time")
game.Players.PlayerAdded:Connect(function(player)
local secs, mins = player:WaitForChild("Seconds"), player:WaitForChild("leaderstats"):WaitForChild("Minutes")
local time_data;
pcall(function() time_data = ds:GetAsync(p.UserId) end)
if time_data then
secs.Value = time_data.seconds
minutes.Value = time_data.minutes
end)
game.Players.PlayerRemoving:Connect(function(player)
local secs, leaderstats = player:FindFirstChild("Seconds"), player:FindFirstChild("leaderstats")
if secs and leaderstats then
ds:SetAsync(player.UserId, {
seconds = secs.Value;
minutes = mins.Value;
})
end
end)
Thank you so much for that,
I really tried it but didn’t work also I’m not that good on scripting so I might didn’t understand you…
So if you just could like put it on uncopylocked game and I get it I will be grateful to you, because I really tried a lot of things and I will try to put my scripts on uncopylocked game so you find that!
P.S. Add the script as a server script in serverscriptservice. Also while working with datastores in the settings, you need to enable “Studio Access to API Services” in the configure settings.