Datastore script not saving yet no errors in the output

Hi, I probably did something really stupid and didn’t realize it when I was scripting but heres the script:

local DSS = game:GetService("DataStoreService")
local PlayerDataDataStore = DSS:GetDataStore("PlayerDataStore")
game.Players.PlayerAdded:Connect(function(player)
	local ls = Instance.new("Folder",player)
	ls.Name = "leaderstats"
	local cash = Instance.new("IntValue",ls)
	cash.Name = "Cash"
	
	local playerUserId = 'Player_'..player.UserId
	local data = PlayerDataDataStore:GetAsync(playerUserId)

	local data = PlayerDataDataStore:GetAsync(player.UserId)
	if data then
		cash.Value = data['Cash']
	else
		cash.Value = 0
	end
end)

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.stats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

game.Players.PlayerRemoving:Connect(function(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = 'Player_'..player.UserId
		PlayerDataDataStore:SetAsync(playerUserId, player_stats)
	end)
	if success then
		print("Successfully saved "..player.Name.."'s data!")
	else
		warn("Failed to save "..player.Name.."'s data!")
	end
end)

It’s not saving for some reason. Any help is appreciated.

Took a quick look and I already noticed something strange. Why are you setting the data variable twice? Based on the rest of your script, something like this should work:

local data = PlayerDataDataStore:GetAsync("Player_"..player.UserId)

Does this fix your problem or give you a different result?

1 Like

Nope, still doesn’t save the currency.
heres the updated script though:

local DSS = game:GetService("DataStoreService")
local PlayerDataDataStore = DSS:GetDataStore("PlayerDataStore")
game.Players.PlayerAdded:Connect(function(player)
	local ls = Instance.new("Folder",player)
	ls.Name = "leaderstats"
	local cash = Instance.new("IntValue",ls)
	cash.Name = "Cash"
	
	local playerUserId = 'Player_'..player.UserId
	local data = PlayerDataDataStore:GetAsync("Player_"..player.UserId)
	if data then
		cash.Value = data['Cash']
	else
		cash.Value = 0
	end
end)

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.stats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

game.Players.PlayerRemoving:Connect(function(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = 'Player_'..player.UserId
		PlayerDataDataStore:SetAsync(playerUserId, player_stats)
	end)
	if success then
		print("Successfully saved "..player.Name.."'s data!")
	else
		warn("Failed to save "..player.Name.."'s data!")
	end
end)

Another possible issue is the name of your folder. Here it looks like you are naming your folder “leaderstats”

However, here you are trying to get something named “stats”

So, changing it to for _, stat in pairs(player.leaderstats:GetChildren()) do might be a fix.

1 Like

Thanks dude I don’t see how I could have missed those. I’m so stupid.