DataStore Not Saving In-Game

My DataStore perfectly saves fine in Studio, but doesn’t in-game what could the problem be?

1 Like

Are you sure it’s not because of API Services being disabled?

image

100% Sure that this isn’t the case!

The script would help to see how the DataStore is being handled :thinking: Otherwise we’re just giving guesses at this point

local dataStoreService = game:GetService("DataStoreService")
local ClicksDataStore = dataStoreService:GetDataStore("ClicksDataStore1")

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

	local Clicks = Instance.new("StringValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leader

	local success, result = pcall(function()
		return ClicksDataStore:GetAsync(player.UserId.."-Clicks")
	end)

	if success then
		Clicks.Value = result
	else
		print("There was an error while getting data")
		warn(result)
		Clicks.Value = 0
	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success,errormessage = pcall(function()
			ClicksDataStore:UpdateAsync(
				player.UserId.."-Clicks",
				function(old) 
					return player:WaitForChild("leaderstats").Clicks.Value 
				end
			)
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
			warn(errormessage)
		end
		
		print(ClicksDataStore:GetAsync(player.UserId.."-Clicks"))
		
		wait(4)
	end
end)

This could be due to the fact that you’re only listening for a BindToClose function which will fire frequently once you stop the simulation inside Studio, but it won’t work ingame, and you’re not listening for the PlayerRemoving event as well

It’s always helpful to listen for these 2, especially in the case if 1 scenario doesn’t load properly

What I’d personally do, is create local functions so that you can connect your events to them way easier like this:

local dataStoreService = game:GetService("DataStoreService")
local ClicksDataStore = dataStoreService:GetDataStore("ClicksDataStore1")

local function LoadData(player)
	local leader = Instance.new("Folder")
	leader.Name = "leaderstats"
	leader.Parent = player

	local Clicks = Instance.new("StringValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leader

	local success, result = pcall(function()
		return ClicksDataStore:GetAsync(player.UserId.."-Clicks")
	end)

	if success then
		Clicks.Value = result
	else
		print("There was an error while getting data")
		warn(result)
		Clicks.Value = 0
	end
end)

local function SaveData(player)
    	local success,errormessage = pcall(function()
		ClicksDataStore:UpdateAsync(
			player.UserId.."-Clicks",
			function(old) 
				return player:WaitForChild("leaderstats").Clicks.Value 
			end
		)
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
		warn(errormessage)
	end
		
	print(ClicksDataStore:GetAsync(player.UserId.."-Clicks"))
		
	wait(4)
end

game.Players.PlayerAdded:Connect(LoadData)
game.Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
        SaveData(player)
	end
end)
1 Like

Try to use this:

local dataStoreService = game:GetService("DataStoreService")
local ClicksDataStore = dataStoreService:GetDataStore("ClicksDataStore1")

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

	local Clicks = Instance.new("StringValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leader

	local success, result = pcall(function()
		return ClicksDataStore:GetAsync(player.UserId.."-Clicks")
	end)

	if success then
		Clicks.Value = result
	else
		print("There was an error while getting data")
		warn(result)
		Clicks.Value = 0
	end
end)

local queue = 0

game:GetService("Players").PlayerRemoving:Connect(function(player)
	queue += 1
	local success, errormessage = pcall(function()
		ClicksDataStore:UpdateAsync(player.UserId.."-Clicks", function(old) 
			return player:WaitForChild("leaderstats").Clicks.Value 
		end)
	end)
	
	queue -= 1
	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
		warn(errormessage)
	end
end)

game:BindToClose(function()
	repeat wait() until queue == 0
end)

Try using PlayerRemoved instead of BindToClose. I think that will work outside of Studio.

API Services just allow data stores and other services to work in studio.

It’s PlayerRemoving, not PlayerRemoved

1 Like
local dataStoreService = game:GetService("DataStoreService")
local ClicksDataStore = dataStoreService:GetDataStore("ClicksDataStore1")

local function save(player)
    local leaderstats = player:FindFirstChild('leaderstats')
    if not leaderstats then return end

    xpcall(function()
		ClicksDataStore:UpdateAsync(
			player.UserId .. "-Clicks",
			function(old) 
				return leaderstats.Clicks.Value 
			end
		)
	end, warn)
end

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

	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leader

	local success, result = pcall(function()
		return ClicksDataStore:GetAsync(player.UserId.."-Clicks")
	end)

	if success then
		Clicks.Value = tonumber(result)
	else
		print("There was an error while getting data")
		warn(result)
	end
end)

game.Players.PlayerRemoving:Connect(save)
game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		save(player)
		wait(11)
	end
end)

I see what the problem is here.
You’re saving the player data when the server is about to close but not when the player leaves.
This leads to the following:
In studio, it will work fine because it’s simulating a small server which closes instantly and at the same time the player (you leaves).

In-game, it will not save because the server will close too long after the last player leaves. Moreover, this script will not work if there are more than 1 player in the server.

So, if you want to make it work you will need to save data for each player when they leave the server using this:

Players = game:GetService("Players")
Players:PlayerRemoving:connect:function()
-- your saving function here
end

I hope this helps, anyways have a nice day/night,
RID3R

First, PlayerRemoving it’s a Players’ event, not a function. If you want to refer to a event, use a dot (.).
Second, Connect is a RBXScriptSignal's function. If you want to set function’s parameters, use parenthesis.

So, use this code instead of yours:

local Players = game:GetService("Players")
Players.PlayerRemoving:Connect(function()
	-- do stuff
end)

Oh yeah sorry, I hadn’t code in Lua for a moment. Thank you for correcting me!