Datastore not saving (or loading?) data

EDIT: The script now magicly works

Hello, I have a datastore script that is not saving data in “Inventory” and “Stats”

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

local Data = {
	Strength = 10;
	Rebirths = 0;
	Cash = 10;
	Speed = 16;
	Damage = 5;
	Health = 100;
	Weight = 1;
	Gems = 0;
	BlueColor = false;
	PinkColor = false;
	GreenColor = false;
	RedColor = false;
	RedSword = false;
	BlueSword = false;
	OmegaSword = false;
	DarkSword = false;
	EquipSword = "default";
	EquipColor = "default"
	}



local playersavetable = {};

local function loadStarterData(Player)
		local Stats = Instance.new("Folder")
		Stats.Name = "Stats"
		Stats.Parent = Player
		local Inventory = Instance.new("Folder")
		Inventory.Name = "Inventory"
		Inventory.Parent = Player
		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 = Stats
			else if type(statvalue) == 'boolean' then
			local intvalue = Instance.new("BoolValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = Inventory
			else if type(statvalue) == 'string' then
			local intvalue = Instance.new("StringValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = Inventory
		end
	end
		end
		end
		end

local function loadData(player, Stats, Inventory)
	local Data = {}
	local Stats = Stats or player.Stats
	local Inventory = player.Inventory
	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.Stats[statname].Value = statvalue
			else if type(statvalue) == "boolean" then
			player.Inventory[statname].Value = statvalue
			else if type(statvalue) == "string" then
			player.Inventory[statname].Value = statvalue
			end
			end
			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.Stats:GetChildren()) do
   if not stat:IsA("Folder") then
       Data[stat.Name] = stat.Value
   end
end
	for _, stat in ipairs(player.Inventory:GetChildren()) do
   if not stat:IsA("Folder") then
       Data[stat.Name] = stat.Value
   end
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)
   
   local Stats = player:WaitForChild("Stats")
   local Inventory = player:WaitForChild("Inventory")

  local RemoveEvent
   RemoveEvent = Players.PlayerRemoving:Connect(function(vplayer)
      if vplayer == player then
         saveData(vplayer, Stats, Inventory)
         RemoveEvent:Disconnect()
      end
   end)
end)

I have tried editing the script for a while now but no luck so far, I have a feeling I made a silly mistake that causes the data in “Inventory” and “Stats” to not save.


An example of the issue is “BlueSword” being sat to true via RemoteEvent and then I leave and rejoin and its back to false (like default)


This issue all started when I tried adding “Inventory” to the datastore as well as stringvalues, before that it worked just fine!

1 Like

I have also tried putting the inventory folder inside the stats folder and putting everything inside the stats folder. The affects of that were the BoolValues not changing, sadly I did not test the IntValues. I would rather not have everything inside the stats folder though because it is very messy.

Have you tried using DataStore2 instead?

Looks like it was solved, but a tip for next time if this starts happening again: try testing in a live server with some debug stuff you can look at. I often have issues with studio not saving or loading, so this might be one of those times when studio acted up.

I also looked at the script and couldn’t find anything that would prevent it from saving, however you could try using HttpService’s JSONEncode and JSONDecode and tostring() to save the data in a string, as I’ve found datastores sometimes have issues with tables.

1 Like

I’m guessing what happened is Roblox had a small outage earlier today causing the script to break. The script was tested in studio when the OP was posted and again when it was edited to be resolved.