DataStore not saving?

Hi guys,

I have recently began learning datastores and have ran into an issue where it is not saving.

I am not sure if it is just an issue with my code, or if the datastore does not work in Studio, but here is my current code.

Summary
local DSS = game:GetService("DataStoreService")
local CurrentDS = DSS:GetDataStore("1")		-- This can be changed to load a new data store

local Players = game:GetService("Players")


Players.PlayerAdded:Connect(function(player)
	
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	coins.Value = 0
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats
	stage.Value = 1
	
	--Sets Data to the Player's ID
	local PlayerID = "Player_"..player.UserId
	

	-- Loads Data
	local save
	
	local suc, err = pcall(function()
		save = CurrentDS:GetAsync(PlayerID)
	end)
	
	if suc then		-- Actually loads the data
		if save then
			coins.Value = save.Coins
			stage.Value = save.Stage
		end
	end
end)

-- SAVE IS NOT WORKING 

-- Saving Player's Data
Players.PlayerRemoving:Connect(function(player)
	local PlayerID = "Player_"..player.UserId
	
	local save = {
		Coins = player.leaderstats.Coins.Value;
		Stage = player.leaderstats.Stage.Value
	}

	local suc, err = pcall(function()
		CurrentDS:SetAsync(PlayerID, save)
	end)
	
	if suc then
		print("Data Saved")
	else
		print("Error during save")
		warn(err)
	end
end)

It will sometimes save, but it seems to randomly work, I’m not really sure what is going on with it. For instance, I did 10 stages of game and stopped the test and it did not save; Then I did the 10 stages again, and it saved.

The last few times that I tried to save something, it did not work.

2 Likes

use save[1]
save[2] and delete the table of save and use this instead of currentds:setasync(playerid, save)
replace it with CurrentDS:SetAsync(PlayerID, player.leaderstats.Coins.Value, player.leaderstats.Stage.Value)

1 Like

You’re assuming PlayerAdded fires on every player, not those in game.

1 Like

Does this problem persist when playing with the game client instead of studio?

1 Like

I am not really sure what you mean by save[1] and save[2]

It only loads the save on PlayerAdded.

It is supposed to Save on PlayerRemoving.

I honestly have not checked whether or not it works in game client; I assumed it would be the same either way.

You still aren’t even checking for current players. PlayerAdded does not fire usually on the first player, you need to check for it.

1 Like

This wouldn’t work. :SetAsync takes 2 required arguments, a key and a value. You are allowed to store tables inside of a DataStore, and if you’re dealing with large amounts of data, you should probably save data using tables rather than using separate keys.

1 Like

How would I check for it? I always assumed playeradded worked whenever someone was added to the game?

Regardless of that, the save is what the current issue is, the pcall prints do not happen.

local suc, err = pcall(function()
		CurrentDS:SetAsync(PlayerID, save)
	end)
	
	if suc then
		print("Data Saved")
	else
		print("Error during save")
		warn(err)
	end

I have seen them in the output a couple of times after stopping a test so I know that they should, but that hardly happens.

Because you’re still assuming that player had code ran for them in the playeradded event, in which case the first player has a 1/8 chance of having the event fire for them/

Datastores don’t save in studio btw

They do, please do not reply with baseless information if you aren’t knowledgeable thanks, it helps prevent misinformation from people who don’t know what they are talking about. OP’s problem is that he isn’t checking for current players that join before PlayerAdded is fired. Datastores work the same way in studio as they do in a live server, the only condition is that API services are enabled in studio’s permissions.

Since when have datastores saved in studio? Is this something new, cause datastores never saved in studio for me.

WHAT wdym omg they do you just have to enable studio acess to api services, thats it

Oh I thought that was just enabling api services for the game not studio lol.

I believe for the game you don’t need to, but it’s recommended to enable it anyway lol, you need to test datastores in studio anyway;

1 Like

Try put the data in a table like this:

local DSS = game:GetService("DataStoreService")
local CurrentDS = DSS:GetDataStore("1")		-- This can be changed to load a new data store

local Players = game:GetService("Players")


Players.PlayerAdded:Connect(function(player)
	
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	coins.Value = 0
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats
	stage.Value = 1
	
	--Sets Data to the Player's ID
	local PlayerID = "Player_"..player.UserId
	

	-- Loads Data
	local save
	
	local suc, err = pcall(function()
		save = CurrentDS:GetAsync(PlayerID)
	end)
	
	if suc then		-- Actually loads the data
		if save then
			coins.Value = save.Coins
			stage.Value = save.Stage
		end
	end
end)

local function createdatatable(player)
    local player_data = {}
for _, data in pairs(player.leaderstats:GetChildren()) do
player_data[data.Name] = data.Value
end
return player_data
end
-- Saving Player's Data
Players.PlayerRemoving:Connect(function(player)
	local PlayerID = "Player_"..player.UserId

	local suc, err = pcall(function()
		CurrentDS:SetAsync(PlayerID,createdatatable(player))
	end)
	
	if suc then
		print("Data Saved")
	else
		print("Error during save")
		warn(err)
	end
end)
1 Like

I believe that it was just not saving for me because I was in Studio, and my only save happened on PlayerRemoving.

It seems to be working fine on a normal game client; and I also added BindToClose as a secondary save.

So how exactly would I get around this? I have always assumed that PlayerAdded always worked, as the script is in ServerScriptStorage, and would load in as the server started and before the player conencted, so it would be working by the time they were loaded in?