I have to rebuy a car to drive it when rejoining, datastore not saving car

You can fix this with a simple if check.

1 Like

@ vxcaie

local function getPlayerData(player)
    local data = {}  -- Initialize the data table -- think this is the problem need this Initialized 
    local success, errormessage = pcall(function()
        local keys = {}
        for _, key in ipairs(CAR_OWNERSHIP_KEYS) do
            keys[key] = player.UserId.."-"..key
        end
        keys.credits = player.UserId.."-credits"
        
        data = myDataStore:GetAsync(keys)
    end)
    return success, data, errormessage
end

Is there a difference between the scripts?

Change this to

if success and data then
        for key, value in pairs(data) do
            local valueObject = leaderstats:FindFirstChild(key)
            if valueObject then
                valueObject.Value = value
            end
        end
    elseif not success and not data then
        print("There was an error while getting "..player.Name.."'s data")
        warn(errormessage)
    end

I got this printed:

Sorry my bad I edited the code should be fixed now

1 Like

How should the code be fixed??

Itā€™s more than hard to show a code fix without having the full program in a case like this. Iā€™m not willing to create this code myself to find a simple syntax error. Iā€™ll try to give you a good logic approach, but youā€™ll need to hammer it out. Youā€™re main problem here was the over use of GetAsync and SetAsync. Try to build your table and save it once. And read your table once. Even if that means reading it to a temp table to then work off of.

1 Like

I tried to use tables for saving, it kinda worked: it saves the credits but when I buy the car the values stays at the value before buying the car, thus having to buy the car againā€¦

I edited the code you can just implement it

I used the older version of the script (the one with the start, slightly modified) but I updated it to the newer one and now nothing weirdly savesā€¦