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

I was trying to make a data script following an alvin blox tutorial, I encountered some problems so I changed it according to some things I saw in the forums but it still doesn’t save, it has access to API services. Thanks in advance

My Terrible Script:

local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("myDataStore")

game:WaitForChild("Players").PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local JumpPower = Instance.new("IntValue")
	JumpPower.Parent = leaderstats
	JumpPower.Name = "JumpPower"
	JumpPower.Value = 0
	
	local data 
	
	local success, errormessage = pcall(function()
		data = MyDataStore:GetAsync(player.UserId.."-JumpPower")
	end)
	
	if success then 
		JumpPower.Value = data
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, errormessage = pcall(function()
		MyDataStore:SetAsync(plr.UserId, plr.leaderstats.JumpPower.Value)	
	end)

	if success then
		print("Data Sucessfully Saved")
	else
		print("There was an error")
		warn(errormessage)
	end
	
	
end)

I can’t really see anything wrong with this. Try resetting the datastore by changing the name, something like myDataStore2, doesn’t really matter.

It is in the error: You can’t store instances in data stores.

You should serialize the instance, and then use JSONEncode (better methods exist but JSONEncode is the simplest).

I can’t actually see any instance assignment, but the error says you’ve done some.

Unrelated issue when you’re calling GetAsync, you’re passing player.UserId.."-JumpPower", but when you’re calling SetAsync you’re only passing player.UserId as the key.

1 Like

You can not store instances in data stores normally. However, you could use a serialization module to store them. I am not going to explain what that is but you should search it up.

Not sure why it says your trying to store an instance but try adding FindFirsrChild() or WaitForChild()

Also add a print before the “SetAsync” like this:

Print(plr.leaderstats.JumpPower.Value)

This is just to see if JumpPower is an instance or integer

Ah thanks for this, I just tried it and the data always returns as 0

this is a new problem that has arisen

1 Like