I need help with UTF-8

So, I recently ran into a problem with UTF - 8. I was trying to create a datastore. Here is the error:

Here is the script:

local DataStoreService = game:GetService("DataStoreService")
local CyberWorldDatastore = DataStoreService:GetDataStore("CyberWorldDatastore")
 
game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder")
	folder.Name = "Data"
	folder.Parent = player
	
	local TimesJoined = Instance.new("IntValue")
	TimesJoined.Name = "TimesJoined" 
	TimesJoined.Parent = folder

	local Credits = Instance.new("IntValue")
	Credits.Name = "Credits" 
	Credits.Parent = folder
	
	local data

	local success, errormessage = pcall(function()
		data = CyberWorldDatastore:GetAsync(player.UserId) 
	end)
	
	if success then
		TimesJoined.Value = data 
		Credits.Value = data
		print("data successfully loaded")
	else
		print("an error occured")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local data = {
		TimesJoined = player.Data.TimesJoined;
		Credits = player.Data.Credits;
	}
	local success, errormessage = pcall(function()
		CyberWorldDatastore:SetAsync(player.UserId, data)		
	end)

	if success then
		print("Data succesfuly saved")
	else
		print("error")
		warn(errormessage)
	end
end)

Does anyone know what a UTF-8 is and how to use it?
Thanks for reading,
Theyoloman1920

Use http service to encode the dictionary into json format then decode it when you need it

2 Likes

You’re problem is you’re trying to store an object instead of its value.

TimesJoined = player.Data.TimesJoined.Value;
Credits = player.Data.Credits.Value;

UTF-8 is just an encoding format. Any characters that can be written can be encoded. But you can’t encode a whole object, as it can be composed of many different data types beyond characters.

1 Like

I believe the issue is on these lines right here.

     local data = {
		TimesJoined = player.Data.TimesJoined;
		Credits = player.Data.Credits;
	 }

If you look at the data your are attempting to store the value instance themselves and not whats inside which is not acceptable as data storage. We can see this by simply looking up what UTF-8 is which is described as this by a quick google search.

"UTF-8 is a variable-width character encoding capable of encoding all 1,112,064 valid character code points in Unicode using one to four one-byte code units. "

It basically is the way that encoding data stores work so when you try to throw the object of the value instead of the value itself roblox data stores do not recognize that kind of data as it is not acceptable as UTF-8 hence it gieves you the error. So you can try switching it out with .Value when you save. Dont forget to mark this as a solution :smile:

1 Like

Roblox automatically encodes data so there is no need

1 Like

I’ve had the same issue as him, but with tables. I had to encode the table then save it.

1 Like

well the issue he is having is witht he values not with json encoding so encoding data doesnt really solve anything in this situation. But like I said before roblox automatically encodes the tables in json format so it is unneeded.

1 Like

Thank you all for replying. I really don’t know which is the right solution, as I have no idea how to do any of these things you told me. I am not that advanced in scripting and I learned about datastores today. Could someone give me a step by step process of what I have to do in order to fix it?

game.Players.PlayerRemoving:Connect(function(player)
	
	local data = {
		TimesJoined = player.Data.TimesJoined.Value; -- You need to encode the value, you can't encode an object
		Credits = player.Data.Credits.Value;
	}
2 Likes

well if you made this code it shouldnt be diffiicult ? it should be relatively simple by just adding a .value at the end of the save data table

1 Like

He obviously watched a tutorial on how to use datastores.

1 Like

Thank you so much! I am sorry @jakebball2019 that I didn’t mark your’s as the solution, but @Exeplex was first…

1 Like

In fact I did, as I thought that it would be the best way to learn about them.

1 Like

Yeah it is, datastores are a hard topic to learn using text so videos are the best way to learn datastores.

1 Like

Now that I did this, the data won’t save at all… I see no error nor any “data successfully saved” message. And when I check, the data are not saved. And yes, I do it from the server side…