Code returns error when saving to datastore

I followed a tutorial on a simple datastore on YouTube and I reached an issue where my output returned a warning regarding saving data.

warn('Something went wrong while saving'..Player.Name.." 's data")

How would I fix my datastore saving?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local DataStore = DataStoreService:GetDataStore('datastore:012')


--//SETTINGS
local StarterData = {
	BarrelPosition = 1	
};

local playersavetable = {};
local AUTO_SAVE_INTERVAL = 10;

--//FUNCTIONS
local function loadStarterData(Player)
	local Placeables = Instance.new("Folder")
	Placeables.Name = "Placeables"
	Placeables.Parent = Player
	for placeableName, placeableValues in pairs(StarterData) do
		if type(placeableValues) == "number" then
			local intvalue = Instance.new("IntValue")
			intvalue.Name = placeableName
			intvalue.Value = placeableValues
			intvalue.Parent = Placeables
		elseif type(placeableValues) == "boolean" then
			local boolvalue = Instance.new("BoolValue")
			boolvalue.Name = placeableName
			boolvalue.Value = placeableValues
			boolvalue.Parent = Placeables
		elseif type(placeableValues) == "string" then
			local stringvalue = Instance.new("StringValue")
			stringvalue.Name = placeableName
			stringvalue.Value = placeableValues
			stringvalue.Parent = Placeables
		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('Getting '..Player.Name.."'s data was successful")
	else
		warn('Something went wrong when loading'..Player.Name.."'s data.")
	end

	if Data then
		for placeableName, placeableValues in pairs(Data) do
			Player.Placeables[placeableName].Value = placeableValues
		end
		print(Player.Name.."'s data has been loaded")
	else
		print(Player.Name..' has no data! Generating new data')
	end
end

local function saveData(Player)
	if RunService:IsStudio() then return end
	local Data = {}
	for _, placeable in pairs(Player.Placeables:GetChildren()) do
		Data[placeable.Name] = placeable.Value
	end
	local s, e = pcall(function()
		DataStore:SetAsync('UserId:'..Player.UserId)
	end)
	if s then
		print(Player.Name.."'s data has been successfully saved")
	else
		warn('Something went wrong while saving'..Player.Name.." 's data")
	end
end

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

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

while true do
	wait(1)
	for _, Player in pairs(Players:GetPlayers()) do
		if tick() - playersavetable[Player] >= AUTO_SAVE_INTERVAL then
			saveData(Player)
			playersavetable[Player] = tick()
		end
	end
end

You didn’t pass the data to save to SetAsync.
For the future issues, it’s worth printing out the error message (it’s stored in a variable e) so you know exactly what went wrong.

1 Like

Beautiful, worked like a charm, Thanks. :slightly_smiling_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.