How to save player's Car After they leave

Hello right now how my datastore is set up when players leave they lose the car they bought. How could i make this not happen. You buy the cars in a gui.

Datastore

local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local datastore = game:GetService("DataStoreService")
local dsl = datastore:GetDataStore("StageSaveSystem")
local DebounceTable = {}

Players.PlayerAdded:Connect(function(plr)
    DebounceTable[plr] = {debounce = false}
    local folder =Instance.new("Folder", plr)
    folder.Name = "leaderstats"

    local Cash = Instance.new("IntValue", folder)
    Cash.Name = "Cash"
    Cash.Value = dsl:GetAsync(plr.UserId) or 200
    local success, errorMessage = pcall(function()
        dsl:SetAsync(plr.UserId, Cash.Value)
    end)

    if success then
        Cash.Changed:Connect(function() 
            local success, errorMessage = pcall(function()
                dsl:SetAsync(plr.UserId, Cash.Value)
            end)
    
            if not success then
                warn(errorMessage)
            end
        end)
    else
        warn(errorMessage)
    end
end)

Players.PlayerRemoving:Connect(function(plr)
    DebounceTable[plr] = nil
end)

replicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar)
    return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value
end

replicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)
    if DebounceTable[player]["debounce"] then
        return 
    end
    DebounceTable[player]["debounce"] = true

if workspace:FindFirstChild(player.Name .. "'s " .. NameOfCar) then
    workspace:FindFirstChild(player.Name .. "'s " .. NameOfCar):Destroy()
end

    local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
    car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,15))
    car.Parent = workspace
    car:MakeJoints()
    car.Name = (player.Name .. "'s " .. NameOfCar)
    wait(5) -- Cooldown
    DebounceTable[player]["debounce"] = false
end)
1 Like

It seems that you only save the amount of cash player has.

You can make a seperate data script or even the same script and make a table with the name of cars the player has.

Once they leave, save the table then once they join back, load it into the player’s inventory.

1 Like

I don’t know how to make a table to save them… i got help along the way with this datastore and i know very little with scripting. This is all i knew to do…

local Players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local dsl = datastore:GetDataStore("CarSaveSystem")

Players.PlayerAdded:Connect(function(plr)
    local folder =Instance.new("Folder", plr)
    folder.Name = "leaderstats"

    local Cars = Instance.new("IntValue", folder)
    Cars.Name = "Cars"
    Cars.Value = dsl:GetAsync(plr.UserId)
    local success, errorMessage = pcall(function()
        dsl:SetAsync(plr.UserId, Cars.Value)
    end)

    if success then
        Cars.Changed:Connect(function() -- oof'd :Connect()
            local success, errorMessage = pcall(function()
                dsl:SetAsync(plr.UserId, Cars.Value)
            end)
    
            if not success then
                warn(errorMessage)
            end
        end)
    else
        warn(errorMessage)
    end
end)

you would save the car names into a table.

then when the player rejoins, load this data and compare it with a folder of cars.
if any names match then give the player the car.

--SAVING
local ownedCars = {"car", "car2", "car3"}
carStore:SetAsync(""..player.UserId, owendCars)


--LOADING
local ownedCars = carStore:GetAsync(""..player.UserId)
local carFolder = game.ServerStorage.CarFolder

for i = 1, #ownedCars do
    local car = carFolder:FindFirstChild(ownedCars[i])
    if(car) then
        --GIVE PLAYER THE CAR MODEL HERE
    end
end

4 Likes