DataStore is not saving

  1. What do you want to achieve? I want to fix the error in my datastore

  2. What is the issue?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("leaderstats")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Data = {
	Strength = 0;
	Rebirths = 0;
	Cash = 1000;
	Speed = 161;
	Items = 0;
	Inventory = nil
}



local playersavetable = {};

local function loadStarterData(Player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = Player
		local Inventory = Instance.new("Folder")
		Inventory.Name = "Inventory"
		Inventory.Parent = leaderstats
		for statname, statvalue in pairs(Data) do
			if type(statvalue) == 'number' then
			local intvalue = Instance.new("IntValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = leaderstats
			else if type(statvalue) == 'boolean' then
			local intvalue = Instance.new("BoolValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = Inventory
		end
	end
		end
		end

local function loadData(player)
	local Data
	local s, e = pcall(function()
	Data = DataStore:GetAsync('UserId'..player.UserId)
	end)
	
	if s then 
		print (player.Name.."Data loaded")
	else
		print(player.Name.."Data failed to load")
	end
	
	if Data then
		for statname, statvalue in pairs(Data) do
			if type(statvalue) == "number" then
			player.leaderstats[statname].Value = statvalue
			end
		end
		print(player.Name.."Data has been loaded")
	else
		print(player.Name.."No data found! generating..")
		end
	end

local function saveData(player)
	--if RunService:IsStudio() then return end
	local Data = {}
	for _, stat in ipairs(player.leaderstats:GetChildren()) do
        Data[stat.Name] = stat.Value
    end
	local s, e = pcall(function()
		DataStore:SetAsync('UserId'..player.UserId, Data)
	end)
		if s then 
	print(player.Name.."Data has been saved")
		else
	warn (player.Name.."Data failed to save"..e)
	end
end

ReplicatedStorage.SaveEvent.OnServerEvent:Connect(function(player)
 saveData(player)	
end)

Players.PlayerAdded:Connect(function(player)
	playersavetable[player] = tick()
	loadStarterData(player)
	loadData(player)
end)

Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

here is my script, when I try to save it outputs " [Value is not a valid member of Folder]" and to explain the Inventory, it starts off with nothing and things get added to it with loot boxes and I want to have the players Items save. Note that the datastore loads data, but does not save.

  1. What solutions have you tried so far? I have looked on the dev forum but found nothing, I tried troubleshooting the script myself but there was no success with that either.

Please let me know if anyone has any questions!

1 Like

I had a similar problem/post
I think you should check it out and read some of the comments. They could help!
https://devforum.roblox.com/t/datastore-not-working/460452

1 Like

Is API services enabled? This allows it to access datastores.

1 Like

Yes it is enabled, the datastore loads data but can not save data.

Okay, nevermind then. Not sure if I can help you :frowning:

Since you are trying to save tables, I would recommend looking at this article for help: Is it possible to create a Table and save/Load it?

Also what line does the output error on? It is a lot easier if you told us where, so we can help.

3 Likes

Sometimes if the datastore is overloaded (via saving too much), it won’t save. I see that you have a save remote event. So I’m guessing triggering the save event is directly controlled at the client at will. That can cause a lot of issues with exploiters taking advantage of the system (just an FYI).

For me, when I test in studio, the game does not successfully save on the player.PlayerRemoving event. When I actually test it in an actual server, it works. Not sure if that is the issue.

That could be because you are the only player in that server, which in this case you would want to use our friendly BindToClose() function which will keep the server up for a maximum of 30 seconds, so you can do whatever is needed before the server shuts down.

Here is an article on the function: DataModel | Documentation - Roblox Creator Hub

2 Likes

Oh, I didn’t know that existed. I’ll implement this right away.

You create a Folder, causing this code to break:

local Data = {}
for _, stat in ipairs(player.leaderstats:GetChildren()) do
       Data[stat.Name] = stat.Value -- Folder does not have a "Value" property
end

You can fix this by detecting if the item is not an inventory folder. If you do plan to save inventory items, you can create a table that stores all the inventory items’ names, and when you load the data, you clone every item (from an inventory folder) using the inventory table.

for _, stat in ipairs(player.leaderstats:GetChildren()) do
   if not stat:IsA("Folder") then
       Data[stat.Name] = stat.Value
   end
end
1 Like