Datastore Problem: Time resets to Zero

When the player plays the MinutesPlayed goes up like usual. But when the player rejoins, their saved number appears on the leaderboard but, then the Minutes played number goes down to 1 instead of adding 1. Sorry for the bad explanation.

Time Added Script:

Datastore:

Game Link: Emote Hangout - Roblox

2 Likes

Do not use normal DataStore if you update it constantly. Try using DataStore2 instead.

That’s a lot of code. First question: Do you want everyone to see someone’s played minutes? If yes, let’s start then.

First create a Script in ServerSriptService. Let’s call it ‘TimeSpendOnPlaying’. For the code write this:

    local DataStoreService = game:GetService("DataStoreService")
    local MinutesPlayedDataStore = DataStoreService:GetDataStore("MinutesPlayed")

    game.Players.PlayerAdded:Connect(function(player)
    	local leaderstats = Instance.new("Folder")
    	leaderstats.Name = "leaderstats"
    	leaderstats.Parent = player

    	local MinutesPlayedInt = Instance.new("IntValue")
    	MinutesPlayedInt.Name = "Minutes Played"
    	MinutesPlayedInt.Parent = leaderstats

    	local data
    	local success, errormessage = pcall(function()
    		data = MinutesPlayedDataStore:GetAsync(player.UserId.."-minutes played")
    	end)

    	if success then
    		MinutesPlayedInt.Value = data
    	else
    		print("There was an error while getting "..player.Name.." data!")
    		warn(errormessage)
    	end
    end)

    game.Players.PlayerRemoving:Connect(function(player)
    	local success, errormessage = pcall(function()
    		MinutesPlayedDataStore:SetAsync(player.UserId.."-minutes played", player.leaderstats["Minutes Played"].Value)
    	end)

    	if success then
    		print(player.Name.." data saved!")
    	else
    		print("There was an error while saving "..player.Name.." data!")
    		warn(errormessage)
    	end
    end)

Once you’re done let’s make a Script located in StarterGUI. Let’s name it ‘MinutesPlayed’ or anything that you want. In the script write this:

while true do
    	wait(60) --There's 60 seconds in one minute.
    	local player = script.Parent.Parent
    	player.leaderstats["Minutes Played"].Value = player.leaderstats["Minutes Played"].Value + 1
    end

That’s everything that I can say. Was it helpful? If yes mark this as a Solution and give it a :heart: ! Thanks!

4 Likes