Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

"Unable to cast value to Object" error datastore. How to fix it?

Asked by
Komas19 34
2 years ago

hello, im trying to make a datastore for my game, but the datastore doesn't work and it shows " Unable to cast value to Object" error in output.

Code (ServerScript)

local DataStoreService = game:GetService("DataStoreService")
local key = "ComputerSimulatorTestingPhase2.0.1"
local myDataStore = DataStoreService:GetDataStore(key)

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local coins = Instance.new("IntValue") --Change points to whatever you want, unless you want to keep it
    coins.Name = "Coins" --Name of the Points, or whatever name you changed it to
    coins.Parent = leaderstats

    local gems = Instance.new("IntValue") --Change wins to whatever you want, unless you want to keep it
    gems.Name = "Gems" --Name of the Wins, or whatever name you changed it to
    gems.Parent = leaderstats

    local pcs = Instance.new("IntValue")
    pcs.Name = "PCs"
    pcs.Parent = leaderstats

    local maxpcs = Instance.new("NumberValue")
    maxpcs.Name = "MaxPCs"
    maxpcs.Parent = leaderstats
    maxpcs.Value = 10

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

    local newmaxpcs = Instance.new("NumberValue")
    newmaxpcs.Name = "NewMaxPCs"
    newmaxpcs.Value = 10
    newmaxpcs.Parent = MaxPCsUpgrade

    local boxes = Instance.new("NumberValue")
    boxes.Name = "Boxes"
    boxes.Value = 0
    boxes.Parent = leaderstats

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId.."-coins", player.UserId.."-gems", player.UserId.."-pcs", player.UserId.."-maxpcs", player.UserId.."-boxes")
    end)

    if success then
        coins.Value = data
        wait(0.5)
        gems.Value = data
        wait(0.5)
        pcs.Value = data
        wait(0.5)
        maxpcs.Value = data
        wait(0.5)
        boxes.Value = data

        print("data loaded")
    else
        warn(errormessage)
        player:Kick(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value, player.UserId.."-gems", player.leaderstats.Gems.Value, player.UserId.."-pcs", player.leaderstats.PCs.Value, player.UserId.."maxpcs", player.leaderstats.MaxPCs.Value, player.UserId.."-boxes", player.leaderstats.Boxes.Value)
    end)

    if success then
        print("Data saved")
    else
        print("An error occured while saving data")
        warn(errormessage)
        player:Kick(errormessage)
    end
end)
0
at what line? aviel101 165 — 2y
0
it doesn't say since it's warn(errormessage) Komas19 34 — 2y

1 answer

Log in to vote
0
Answered by
aviel101 165
2 years ago
Edited 2 years ago

try changing your PlayerRemoving pcall function to this

local success, errormessage = pcall(function()
    myDataStore:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
    myDataStore:SetAsync(player.UserId.."-gems", player.leaderstats.Gems.Value)
    myDataStore:SetAsync(player.UserId.."-pcs", player.leaderstats.PCs.Value)
    myDataStore:SetAsync(player.UserId.."maxpcs", player.leaderstats.MaxPCs.Value)
    myDataStore:SetAsync(player.UserId.."-boxes", player.leaderstats.Boxes.Value)
end)

also remove those lines from PlayerAdded

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId.."-coins", player.UserId.."-gems", player.UserId.."-pcs", player.UserId.."-maxpcs", player.UserId.."-boxes")
    end)

    if success then
        coins.Value = data
        wait(0.5)
        gems.Value = data
        wait(0.5)
        pcs.Value = data
        wait(0.5)
        maxpcs.Value = data
        wait(0.5)
        boxes.Value = data

        print("data loaded")
    else
        warn(errormessage)
        player:Kick(errormessage)
    end

and change them with those

    local success, errormessage = pcall(function()
        coins.Value = myDataStore:GetAsync(player.UserId.."-coins")
        gems.Value = myDataStore:GetAsync(player.UserId.."-gems")
        pcs.Value = myDataStore:GetAsync(player.UserId.."-pcs")
        maxpcs.Value = myDataStore:GetAsync(player.UserId.."-maxpcs")
        boxes.Value = myDataStore:GetAsync(player.UserId.."-boxes")
    print("data loaded")
    end)

    if not success then
        warn(errormessage)
        player:Kick(errormessage)
    end

Explanation: The reason it didn't work is because everytime you call SetAsync you have to call it with only 2 arguments :SetAsync(DataId, NewValue)

and everytime you call GetAsync you have to call it with only 1 Argument :GetAsync(DataId)

what you did is calling them with more than the arguments required, that's what caused the error

0
which pcall functions? the one is PlayerAdded or the other one? Komas19 34 — 2y
0
the one in PlayerAdded or PlayerRemoving? Komas19 34 — 2y
0
thx it works Komas19 34 — 2y
0
i might respond to the explaination tomorrow Komas19 34 — 2y
Ad

Answer this question