Unable to cast value to Object

I’ve used this system to save data in a few of my games and it’s been fine, but today when I’m testing the game it keeps giving me the error “Unable to cast value to Object”

local Players = game:GetService("Players")
local dss = game:GetService("DataStoreService")
local DataStoreGold = dss:GetOrderedDataStore("Gold")
local function leaderboardSetup(player)

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

	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Parent = player.leaderstats
	gold.Value = DataStoreGold:GetAsync(player.UserId, player.leaderstats.Gold.Value) or 0

end

local function playerleft(player)
	DataStoreGold:SetAsync(player.UserId, player.leaderstats.Gold.Value)
end

Players.PlayerAdded:Connect(leaderboardSetup)
Players.PlayerRemoving:Connect(playerleft)

image

I don’t know why this is happening and would appreciate it if someone could help

You only need 1 value, the userid for GetAsync. By using GetAsync you are already getting it from the gold datastore

2 Likes

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