Why isn't this datastore script working?

Hi! :grin: I’m making a data and inventory saving system for my game. I am quite new to using tables and datastores and I don’t really understand why my code doesn’t work.

There is a “Tools” and “Trails” folder in ReplicatedStorage, the script loops through every item in those folders and saves/loads it.

I’ve tried looking at the devforum but there aren’t really any threads similar to mine which have this issue. The script adds the inventory folders and item values to the player, but it doesn’t get saved/loaded. No errors in the output.

Any help is appreciated :smiley:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Feb2020")
local plrsLeft = 0

function Save(player)
	print("Started saving data")
	local key = "Player_" ..player.UserId
	
	local save = {
		["Money"] = player.leaderstats.Alphabux.Value;
		["Wins"] = player.leaderstats.Wins.Value;		
		
		["Level"] = player.data.Level.Value;
		["XP"] = player.data.XP.Value;
		["AcidSurvival340k"] = player.data.AcidSurvival340k.Value;	
	}
	
	for i,trail in pairs(player.Inventory.Trails:GetChildren()) do
		save[trail.Name] = trail.Value
	end
	
	for i,tool in pairs(player.Inventory.Tools:GetChildren()) do
		save[tool.Name] = tool.Value
	end
	
	local success, err = pcall(function()
		print("pcall started")
		DataStore:SetAsync(key, save)
	end)
	
	print("pcall ended")
		
	if not success then
		warn("Failed to save data!"..tostring(err))
		return
	else
		print("Successfully saved data!")
	end
	
	plrsLeft = plrsLeft - 1
end



local function Load(player)
	player.CharacterAdded:Connect(function(character)
		player.TeamColor = BrickColor.new("Medium stone grey")
		local role = player:GetRoleInGroup(4519654)
		if role ~= "Guest" then
			player.Inventory.Trails.AlphaPlanetTrail.Value = true
		end
	end)
	
	-- leaderstats
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local money = Instance.new("IntValue", leaderstats)
	money.Name = "Alphabux"
	money.Value = 100

	local wins = Instance.new("IntValue", leaderstats)
	wins.Name = "Wins"
	wins.Value = 0
	
	local afk = Instance.new("BoolValue", player)
	afk.Name = "AFK"
		
	-- inventory
	local inventory = Instance.new("Folder", player)
	inventory.Name = "Inventory"
	
	-- trails
	local trails = Instance.new("Folder", inventory)
	trails.Name = "Trails"
	
	for i,trail in pairs(game.ReplicatedStorage.Trails:GetChildren()) do
		local trailValue = Instance.new("BoolValue", trails)
		trailValue.Name = trail.Name
	end	
	
	-- tools
	local tools = Instance.new("Folder", inventory)
	tools.Name = "Tools"
	
	for i,tool in pairs(game.ReplicatedStorage.Tools:GetChildren()) do
		local toolValue = Instance.new("BoolValue", tools)
		toolValue.Name = tool.Name
	end	
	
	-- data
	local data = Instance.new("Folder", player)
	data.Name = "data"

	local level = Instance.new("IntValue", data)
	level.Name = "Level"
	level.Value = 1
	
	local xp = Instance.new("IntValue", data)
	xp.Name = "XP"
	
	local acidSurvival340k = Instance.new("BoolValue", data)
	acidSurvival340k.Name = "AcidSurvival340k"
		
	local key = "Player_" ..player.UserId
	
	local SavedData
	
	local success, err = pcall(function()
		SavedData = DataStore:GetAsync(key)
	end)
	
	if not success then
		warn("Failed to load data!"..tostring(err))
		return
	else
		print("Successfully loaded data!")
	end
	
	if SavedData then
		money.Value = SavedData.Money
		wins.Value = SavedData.Wins
		
		for i,trail in pairs(player.Inventory.Trails:GetChildren()) do
			trail.Value = SavedData[trail.Name]
		end
		
		for i,tool in pairs(player.Inventory.Trails:GetChildren()) do
			tool.Value = SavedData[tool.Name]
		end
		
		level.Value = SavedData.Level
		xp.Value = SavedData.XP
		acidSurvival340k.Value = SavedData.AcidSurvival340k
	else
		Save(player)
	end
	
	player.data.XP.Changed:Connect(function()
		if player.data.XP.Value >= (player.data.Level.Value * 200) then
			player.data.XP.Value = player.data.XP.Value - (player.data.Level.Value * 200)
			player.data.Level.Value = player.data.Level.Value + 1
			player.leaderstats.Alphabux.Value = player.leaderstats.Alphabux.Value + 50
			print(player.Name.." leveled up!")
			game.ReplicatedStorage.LevelUp:FireClient(player, 50)
		end
		wait(1)
	end)
	
	plrsLeft = plrsLeft + 1
end



Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		Save(player)
	end
	
	repeat
		wait()
	until
	plrsLeft == 0
	
	print("All data saved")
end)	

This looks fine to me (if i read it correctly). Do you have any errors or anything related to your script in your output? Make sure your game is published and the “Enable Studio Access to API Services” in the game settings is enabled if not already.

1 Like

Hi! Thanks for the reply. No errors in the output, the game is published and API access is enabled.

Your datastore calls are wrapped in a pcall, which will stop the errors from showing if there are any. In the loading you handle this correctly because there is a warning on a failure, but you don’t do this when saving.

1 Like

Ohh. right. My mistake, thanks for pointing it out. I just added this line -

	if not success then
		warn("Failed to save data!"..tostring(err))
		return
	end

and there is no warn about any errors in the output. Anyone else have any ideas on how to fix it/whats wrong with it?

Dumb mistake found. The script goes thorugh the trails to load the tools, but it should go through all of the tools to load their values.