"Unable to cast Value to Object" Datastore Table Error

Hello everyone,

I have a script that stores a table in a Datastore, and for some reason, today, I am getting this error and can’t pinpoint the exact cause.
image

This is the script:

local dataStoreService = game:GetService("DataStoreService")
local TreeDataStore = dataStoreService:GetDataStore("TreeDataStore")

local Save = game.Workspace.SaveTree.ClickDetector

local TreeData = {}

game.Players.PlayerAdded:Connect(function(player)
	table.insert(TreeData, {R = 255, G = 0, B = 0})

	local success, TreeData = pcall(function()
		return TreeDataStore:GetAsync(tostring(player.UserId).."-TreeData", TreeData)
	end)
	if success then
		print("Retrieved TreeData Successfully")
	else
		warn("Error: ".. TreeData)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMessage = pcall(function()
		TreeDataStore:SetAsync(tostring(player.UserId).."-TreeData", TreeData)
	end)
	if errorMessage then
		warn("Error: ".. errorMessage)
	else
		print("TreeData has been Saved.")
	end
end)

Thank you in advance.

1 Like

Prior to having it save, can you please print out the value of TreeData?

You shouldn’t be passing a second parameter in the GetAsync thing (technically there is a time and place for a second parameter, which is the DataStoreGetOptions, however it isn’t applicable in this case).

Reduce line 12 to just

return TreeDataStore:GetAsync(tostring(player.UserId).."-TreeData")

and see if it resolves your issue

1 Like

Thank you, this worked. It’s quite peculiar as I recall it working just fine yesterday.
image