DataStore Problem in my script

I am trying how to do datastore but when I do what others do, the saving system keeps telling me that data is not saved. For about 30 times. I think that is not normal. So, I want to know what’s wrong with my script.

local DataStore = game:GetService("DataStoreService")
local SaveEvent = game.Workspace.Events.SaveData
local LoadEvent = game.Workspace.Events.LoadData
local DiedEvent = game.Workspace.Events.PlayerDied
local ResetData = game.Workspace.Events.ResetData
local PlayerGameDatas = game.ServerStorage.PlayerDatas
local Data = DataStore:GetDataStore("DATA")

function SaveData(Player) --- Overwrite a new save ---
	local UnsavedData = PlayerGameDatas:WaitForChild(Player.Name)
	repeat
		wait(1)
		local Success,ErrMsg = pcall(function()
			Data:SetAsync("Player_"..Player.UserId,UnsavedData)
		end)
		if Success == true then
			print("Pro")
		else
			print("NotPro")
		end
	until Success
end

function LoadData(Player) --- Load Saves from datastore ---
	local PlayerData = Data:GetAsync("Player_"..Player.UserId)
	if PlayerData then
		PlayerData.Parent = PlayerGameDatas
		print("Save Loaded")
	else
		local Structure = script.DataStructure:Clone()
		Structure.Parent = PlayerGameDatas
		Structure.Name = Player.Name
		print("New Save Created")
	end
end

function ResetData(Player) --- Set everything to default ---
	
end

SaveEvent.OnServerEvent:Connect(function(Plr)
	SaveData(Plr)
end)

LoadEvent.OnServerEvent:Connect(function(Plr)
	LoadData(Plr)
end)
3 Likes

What exactly is it saying? If it’s saying “NotPro”, then it’s likely because you used repeat, and it never got “Success”.

1 Like

Yes, I used that to make sure the data is saved.
I used a repeat to retry saving.

1 Like

Then what’s the issue exactly? Wouldn’t that mean that it’s working as it’s intended?

1 Like

Can you show us your output if there are any errors at all?

1 Like

There is no error at all, but it keeps telling me that the data wasn’t saved.

1 Like

There is no errors in the output, but it tells me that it didn’t save successfully.

1 Like

What exactly is it printing or is it even printing? If it is printing “NotPro”, then you should have it output the error.

1 Like

Ok, umm. I printed the error message. It says: 104: Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters.
Then how do I supposed to load buildings that players made???
Or maybe I should put the whole folder into a table then save it?

1 Like

This needs to say.Value otherwise you are sending a gameobject to a datastore as it told you it only allows UTF-8 characters.
Needs to be

local UnsavedData = PlayerGameData:WaitForChild(Player.Name).Value

You have a few problems. The biggest is probably that your trying to save lua userdata. Any roblox class object; numberValue, stringValue, part, model, etc. is userdata.
And all data in roblox datastores are saved as strings in JSON format. Attempting to encode any userdata as a JSON string will return ‘null’. So if you’re not getting an error, you’re just saving nothing.

type(Instance.new('NumberValue')) -- userdata; Cannot be saved
type({stats = {}, points=100 }) -- table; Can be saved

So, I should just put a folder into a table then save the table?

Alright, so you mean something like?

object = Instance.new('NumberValue')
tab = {points = object}

That’s a good idea for sure. Although it doesn’t really work out. You have a table which can be saved, but you’re still relying on userdata in some way. So this in JSON string format, would look like- {“points”:null}. The table gets saved, but the userdata is lost.

You’ll want to have some conversion from numberValues, stringValues, folders, etc to lua datatypes.

Here's some functions to help with converting the conversions.
local convertToTable
convertToTable = function(robloxObject)
	local tab={}
	for _,child in pairs(robloxObject:GetChildren()) do
		if child:IsA('Folder') or child:IsA('Model') then
			tab[child.Name] = convertToTable(child)
		elseif child:IsA('ValueBase') then
			tab[child.Name] = child.Value
		end
	end
	return tab
end


local convertToRobloxObject
convertToRobloxObject = function (luaType)
	local instance
	
	if type(luaType)=='table' then
		instance=Instance.new('Folder')
		for name, value in pairs(luaType) do
			local entry = convertToRobloxObject(value)
			entry.Name=name
			entry.Parent=instance
		end
		
	elseif type(luaType)=='number' then
		instance=Instance.new('NumberValue')
		instance.Value=luaType
		
	elseif type(luaType)=='boolean' then
		instance=Instance.new('BoolValue')
		instance.Value=luaType
		
	elseif type(luaType)=='string' then
		instance=Instance.new('StringValue')
		instance.Value=luaType
	end
	return instance
end

In the end, you might find it easier to only rely on lua tables for data. But hope this helps.

Look at this topic that has the same error, and the solution: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters - #7 by spaceconsole

The first thing I’d do when I receive an error and can’t figure it out, is search that exact error message on google. It’s a good way to go about things. Hope I was of some help.

So, datasaving like tycoon or something. You need to give information of a userdata instead of userdata?

Yup, saves values not roblox objects.