Roblox DataStores using pcall() keep returning "Unable to cast value to object"

So recently after a new Roblox Update all of my datastores in all of my games that uses them keeps returning “Unable to cast value to object” and I’ve spent 5 hours trying to fix this but I think it’s a Roblox issue.

Code I usually use for loading DataStores and saving them (Even this is spitting out the same error)

local MainData = game:GetService("DataStoreService"):GetDataStore("LSData")
local Littlebuck = 0

game.Players.PlayerAdded:Connect(function(player)
    wait(0.1)
    local Success, Result = pcall(function()
        return MainData:GetAsync(tostring(player.UserId),"LSData")
    end)
    if Success then
        if Result then
            if Result.Littlebucks then -- LITTLEBUCKS
                print(Result.Littlebucks)
            else
                Littlebuck = 1000
            end
        else
            Littlebuck = 1000
        end
    end
    
    if not Success then
        print(Result.. " data store")
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Success, Result = pcall(function()
        return MainData:GetAsync(tostring(player.UserId),"LSData")
    end)
    
    if Success then
        MainData:SetAsync(player.UserId, {    
            Littlebucks = Littlebuck,
        })    
        
        wait(0.1)

    end
    
    if not Success then
        print(Result)
    end
end)

Errors in all of my games is the same all of a sudden.



Anyone else experiencing this? Usually constantly happens in-game and not in Roblox Studio.

1 Like

Never mind, I resolved it the issue.

For anyone coming here later, the problem was like this:

MainData:GetAsync(tostring(player.UserId),"LSData")

GetAsync should only take the key and nothing else (unless you use a scope). Removing “LSData” as a parameter would likely fix the issue.

1 Like

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