Error while making Data Store

Hi, I’m repliicated, and yesterday I was following a tutorial about making a datastore. After doing everything, I was excited to try it out. Then, when I “left” the game, it warned me in the output:

Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters.

I would like help, because I don’t think players will be happy if they can’t save their data in my mediocre Break Your Neck Joint Simulator! (lol).

Here is my code if it helps:

local DataStoreService = game:GetService("DataStoreService")
local CountClicks = DataStoreService:GetDataStore("NeckBroken")



game.Players.PlayerAdded:Connect(function(player)
	local startingclicks = 0

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = player

	local clicks = Instance.new("IntValue")
	clicks.Name = 'Times Broken Neck'
	clicks.Value = startingclicks
	clicks.Parent = leaderstats
	
	local PlayerId = "Player_" .. player.UserId
	
	
	local data
	local success, errormessage = pcall(function()
		data = CountClicks:GetAsync(PlayerId)
	end)
	
	if success then
		clicks.Value = data
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local PlayerId = "Player_" .. player.UserId
	local data = player.leaderstats["Times Broken Neck"]
	local success, errormessage = pcall(function()
		CountClicks:SetAsync(PlayerId, data)
	end)
	if success then
		print("Data successfully saved!")
	else
		print("something wrong bro, this is the problem:")
		warn(errormessage)
	end
end)

Hey there!

The problem is on this line:

as you are trying to save an Instance, instead of it’s value.
If you change the line to following:

local data = player.leaderstats["Times Broken Neck"].Value

the error should be gone.

The loading data stores part seems fine.

I hope this was able to resolve your issue!
Have a nice day!

2 Likes

It worked, but only once and now I’m stuck having 1 broken neck lol

Maybe I should have watched the whole tutorial and not stop 10 minutes before…

Edit: It worked when I went to the actual game! Thanks so much!!

Hey there!

I see you are having problems with testing things such as Data Stores inside of the Studio Solo Play.
You can read my tutorial that I made about this.

1 Like