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

BindToClose fires when the server shutdowns. It’s needed because when the server shutdowns and everyone is kicked, PlayerRemoving isn’t fired for the player’s that were kicked, so we have to manually save their data.

1 Like

So it says that you own the vehicle, but it won’t create the vehicle for you? You can also remove the prints in the saving script, forgot to remove them when I sent the new one to you.

1 Like

Pretty much yes, I removed the printing sections in both scripts, and it prints nothing related.

Are those cars the “Punto”?
image

1 Like

No. If I spawn other vehicles, it does the same.

Did this spawning system use to work before? Also, I feel like I’m missing some details. Is the car spawning but not the correct one? Or is it not spawning at all?

I’ll give it a shot … Think the key here is going for one SetAsync vs many.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetOrderedDataStore("myDataStore")

local CAR_OWNERSHIP_KEYS = {
    "JZA80own", "F40own", "FD7own", "GT3RS991own",
    "E30own", "RS3own", "Z370own", "PUNTOown",
    "TTRSown", "POLOown", "RC206own", "RLY206own", "AUDIF1own"
}

local function getPlayerData(player)
    local data = {}
    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

local function setPlayerData(player, data)
    local success, errormessage = pcall(function()
        local keysAndValues = {}
        for key, value in pairs(data) do
            keysAndValues[player.UserId.."-"..key] = value
        end
        
        myDataStore:SetAsync(keysAndValues)
    end)
    return success, errormessage
end

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

    for _, key in ipairs(CAR_OWNERSHIP_KEYS) do
        local value = Instance.new("BoolValue")
        value.Name = key
        value.Parent = leaderstats
    end

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

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

game.Players.PlayerRemoving:Connect(function(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        local data = {}
        for _, key in ipairs(CAR_OWNERSHIP_KEYS) do
            local valueObject = leaderstats:FindFirstChild(key)
            if valueObject then
                data[key] = valueObject.Value
            end
        end
        data.credits = leaderstats.credits.Value
        
        local success, errormessage = setPlayerData(player, data)
        if success then
            print(player.Name.."'s data was successfully saved!")
        else
            print(player.Name.."'s data had an error when saving")
            warn(errormessage)
        end
    end
end)
1 Like

Saving has already been solved earlier:

We are just working on the buying/spawning script now (if you want to contribute).

I’m back now. The spawning system never did work prior to posting this,

Oh, that’s great, I didn’t see a solved.

Ohhhh, well that explains a lot. I thought that it always worked, so I was extremely confused. Where is this script located?

Both the scripts are located in ServerScriptService.

I will test the script right now.

Okay, try this script:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local fq400 = ReplicatedStorage.Database.LancerEvo8
local supra4 = ReplicatedStorage.Database.SupraMK4
local f458 = ReplicatedStorage.Database.F40
local fn7 = ReplicatedStorage.Database.HondaCivicFD7
local gt86 = ReplicatedStorage.Database.GT86
local GT3RS1 = ReplicatedStorage.Database["991GT3RS"]
local E30M3 = ReplicatedStorage.Database.E30M3
local RS3 = ReplicatedStorage.Database.RS3
local Z370 = ReplicatedStorage.Database.Z370
local Punto = ReplicatedStorage.Database.Punto
local TTRS = ReplicatedStorage.Database.TTRS
local VWWRC = ReplicatedStorage.Database.PoloWRC
local RC206 = ReplicatedStorage.Database.RC206
local Rl206 = ReplicatedStorage.Database.RLY206
local vagaudi1f = ReplicatedStorage.Database.AudiF1

--//Functions
local function ValidateSpawn(value, price, car)
	local player = value.Parent.Parent

	if value.Value then
		local newCar = car:Clone()
		newCar:PivotTo(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
		newCar.Parent = workspace
		
		print("Vehicle spawned")
	elseif player.leaderstats.credits.Value >= price then			
		player.leaderstats.credits.Value -= price

		value.Value = true
		
		local newCar = car:Clone()
		newCar:PivotTo(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
		newCar.Parent = workspace
		
		print("Vehicle spawned")
	end
end

ReplicatedStorage.Events.FQ400.OnServerEvent:Connect(function(player)
	if player:IsInGroup(12020467) then
		local spawned = fq400:Clone()
		spawned:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
		spawned.Parent = workspace
	end
end)

ReplicatedStorage.Events.SupraMK4.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.JZA80own, 35000, supra4)
end)

ReplicatedStorage.Events.F40.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.F40own, 100000, f458)
end)

ReplicatedStorage.Events.HondaCivicFD7.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.FD7own, 15000, fn7)
end)

ReplicatedStorage.Events.GT86.OnServerEvent:Connect(function(player)
	local spawned = gt86:Clone()
	spawned:PivotTo(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	spawned.Parent = workspace
end)

ReplicatedStorage.Events.GT3RS991.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.GT3RS991own, 165000, GT3RS1)
end)

ReplicatedStorage.Events.E30M3.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.E30own, 750000, E30M3)
end)

ReplicatedStorage.Events.RS3.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.RS3own, 30000, RS3)
end)

ReplicatedStorage.Events.Z370.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.Z370own, 65000, Z370)
end)

ReplicatedStorage.Events.Punto.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.PUNTOown, 7950, Punto)
end)

ReplicatedStorage.Events.TTRS.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.TTRSown, 65000, TTRS)
end)

ReplicatedStorage.Events.PoloWRC.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.POLOown, 75000, VWWRC)
end)

ReplicatedStorage.Events.RC206.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.RC206own, 12500, RC206)
end)

ReplicatedStorage.Events.RLY206.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.RLY206own, 112500, Rl206)
end)

ReplicatedStorage.Events.AudiF1.OnServerEvent:Connect(function(player)
	ValidateSpawn(player.leaderstats.AUDIF1own, 425000, vagaudi1f)
end)

I have put it as the CarSpawns script.

When I go into the game, I get this:

Also, it resets my data somehow.

By my looks the error is pretty minor tho

Ya, I did that off the top of my head. Looks like he knows enough to get that working …
More of an outline.

I’m not sure if I could fix this by tommorow… Else there’s no point.