Unable to cast value to Object

im trying to make a datastore script which stores a players redeemed codes, however, im getting the error unable to cast value to object and i cant find out whats wrong


local DS = game:GetService("DataStoreService"):GetDataStore("Codes")

game.Players.PlayerAdded:Connect(function(player)
	wait()
	local plrKey = "id_"..player.UserId
	local getSaved = DS:GetAsync(plrKey)
	if getSaved then
		for i, v in pairs(getSaved) do
			local temp = Instance.new("BoolValue", player.Codes)
			temp.Name = v
		end
	else
		local ValuesForSaving = {}
		for i, v in pairs(player.Codes:GetChildren()) do
			table.insert(ValuesForSaving, v.Name)
		end
		DS:GetAsync(plrKey, ValuesForSaving) -- problematic line
		
	end
end)

game.Players.PlayerRemoving(function(player)
	local savevalues = {}
	
	for i, v in pairs(player.Codes:GetChildren()) do
		table.insert(savevalues, v.Name)
	end
	DS:GetAsync("id_"..player.UserId)
end)

:GetAsync will retrieve the latest value that is stored on the key within the database. You are trying to save data to that key so you should use either :SetAsync or :UpdateAsync.

1 Like

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