Data not saving | Help needed!

I have a data store script that saves data, basically the in-game seconds. They increase by 1 after second and I want to save them.

My whole script:

--// Variables, Services and functions

local dataStore = game:GetService("DataStoreService"):GetDataStore("BucksDataStore")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local gameSeconds = ReplicatedStorage:WaitForChild("GameSeconds")

local playersLeft = 0

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	
	     playersLeft = playersLeft + 1
	
	     local leaderstats = Instance.new("Folder")
	     leaderstats.Name = "leaderstats"
	     leaderstats.Parent = player    
	
	     local playerName = Instance.new("StringValue")
	     playerName.Name = "Name"
	     playerName.Value =  player.Name
	     playerName.Parent = leaderstats

	     local Teir = Instance.new("StringValue")
	     Teir.Name = "Teir"
	     Teir.Value = "o"
	     Teir.Parent = leaderstats
	
         local timeSpent = Instance.new("IntValue")
	     timeSpent.Name = "Game Time"
	     timeSpent.Value = gameSeconds.Value
	     timeSpent.Parent = leaderstats
	
	     gameSeconds.Changed:Connect(function()
		        player.leaderstats["Game Time"].Value = gameSeconds.Value
	    end)
	
	    	-- Data stores
	
	local player_data

	pcall(function()
		player_data = dataStore:GetAsync(player.UserId.."-Time")
	end)
	
	if player_data ~= nil then
		timeSpent.Value = player_data 
	else
		-- New player
		timeSpent.Value = 0
	end
	
end)	
	
local bindableEvent = Instance.new("BindableEvent")

Players.PlayerRemoving:Connect(function(player)
	
	pcall(function()
		dataStore:SetAsync(player.UserId.."-Time",player.leaderstats["Game Time"].Value)
		print("Saved player's data")
		playersLeft = playersLeft -1
		bindableEvent:Fire()
	end)
	
end)

game:BindToClose(function()
	while playersLeft > 0 do
		bindableEvent.Event:Wait()
	end
end)

It even prints Data saved and API services are turned on as well as I test this in the actual game. What could be the issue?

image

1 Like

Try test it in studio and look at the output

1 Like

remove the pcall(function() end) and see what it prints out

1 Like

No…
This is a bad way to handle pcalls. You’re using them to make your data saving efficient and robust, not scrub off errors and let data saving failures happen blindly.

local SaveSuccess, CallResult = pcall(function()
    return player_data = dataStore:GetAsync(player.UserId.."-Time")
end

local Retries = 0
local MaxRetries = 5
while not SaveSuccess do
    Retries = Retries + 1
    if Retries == MaxRetries then break end
    SaveSuccess, CallResult = pcall(function()
        return player_data = dataStore:GetAsync(player.UserId.."-Time")
    end
    wait(1)
end

if SaveSuccess then
    print("Data Saved")
else
    print(CallResult)
end

And due to how you initially used pcalls, you simply missed all the error messages returned from the DataStore. Just apply the code I wrote to all your data store methods.

Edit:
I just realized that the code I made was to load the player data, not save. My bad…

2 Likes