Unable to cast value to Object

I was trying to make a DataStore script for a simulator, but then I got an error in the output that said “Unable to cast value to Object”

I was using a tutorial, but I kept double checking, and my code was the same as theirs.

Here’s my script:

local DS = game:GetService("DataStoreService"):GetDataStore("Store-1")

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	
	local plrkey = "id_"..plr.UserId
	
	local savevalue1 = plr.leaderstats.Points
	
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		savevalue1.Value = GetSaved[1]
	else
		local ValuesToSave = {savevalue1.Value}
		DS:GetAsync(plrkey, ValuesToSave)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local saveItems = {plr.leaderstats.Points.Value}
	DS:SetAsync("id_"..plr.UserId, saveItems)
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetChildren()) do
		plr:Kick()
	end
end)

Also, the error was on line 15, which was

		DS:GetAsync(plrkey, ValuesToSave)

You can’t actually save properties of an object to a variable. Instead, try rewriting lines 14 and 15 like this

local ValuesToSave = {savevalue1}
DS:GetAsync(plrkey, ValuesToSave.Value)

Hopefully this helps.

Okay, so there’s no more errors in the output, but it doesn’t save anything,

Okay, so turns out on line 15 I just had to change :GetAsync to :SetAsync.

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