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

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a car buying system, so people could drive in an immersive world.

  2. What is the issue? Include screenshots / videos if possible!
    The cars aren’t saving, and instead when rejoining I need to rebuy the car, unless I will buy the car which I can just peacefully spawn it, and then rejoin and it’ll break.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to see if I didn’t write something, since each car has it’s own value and I have to copy them over and over. I tried to also look for YouTube videos related to buying systems, but they did little to no help at all.

Each car has it’s own value. Let’s say I have a value called FORD car, it’s named FORDown (pretty much any car value has lowercase “own” in the end), and then a data variable called dataFORD, and this applies for 14 cars. If you didn’t understand what I said, I think you should look into the script.

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

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

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

	local JZA80own = Instance.new("BoolValue")
	JZA80own.Name = "JZA80own"
	JZA80own.Parent = leaderstats

	local F40own = Instance.new("BoolValue")
	F40own.Name = "F40own"
	F40own.Parent = leaderstats

	local FD7own = Instance.new("BoolValue")
	FD7own.Name = "FD7own"
	FD7own.Parent = leaderstats

	local GT3RS991own = Instance.new("BoolValue")
	GT3RS991own.Name = "GT3RS991own"
	GT3RS991own.Parent = leaderstats

	local E30own = Instance.new("BoolValue")
	E30own.Name = "E30own"
	E30own.Parent = leaderstats

	local RS3own = Instance.new("BoolValue")
	RS3own.Name = "RS3own"
	RS3own.Parent = leaderstats

	local Z370own = Instance.new("BoolValue")
	Z370own.Name = "Z370own"
	Z370own.Parent = leaderstats

	local PUNTOown = Instance.new("BoolValue")
	PUNTOown.Name = "PUNTOown"
	PUNTOown.Parent = leaderstats

	local TTRSown = Instance.new("BoolValue")
	TTRSown.Name = "TTRSown"
	TTRSown.Parent = leaderstats

	local POLOown = Instance.new("BoolValue")
	POLOown.Name = "POLOown"
	POLOown.Parent = leaderstats

	local RC206own = Instance.new("BoolValue")
	RC206own.Name = "RC206own"
	RC206own.Parent = leaderstats

	local RLY206own = Instance.new("BoolValue")
	RLY206own.Name = "RLY206own"
	RLY206own.Parent = leaderstats

	local AUDIF1own = Instance.new("BoolValue")
	AUDIF1own.Name = "AUDIF1own"
	AUDIF1own.Parent = leaderstats

	local dataJZA80
	local dataF40
	local dataFD7
	local dataGT3991RS
	local dataE30
	local dataRS3
	local dataZ370
	local dataPUNTO
	local dataTTRS
	local dataPOLO
	local dataRC206
	local dataRLY206
	local dataAUDIF1
	local creditdata
	local success, errormessage = pcall(function()
	    creditdata = myDataStore:GetAsync(player.UserId.."-credits")
		dataJZA80 = myDataStore:GetAsync(player.UserId.."-JZA80own")
		dataF40 = myDataStore:GetAsync(player.UserId.."-F40own")
		dataFD7 = myDataStore:GetAsync(player.UserId.."-FD7own")
		dataGT3991RS = myDataStore:GetAsync(player.UserId.."-GT3991RSown")
		dataE30 = myDataStore:GetAsync(player.UserId.."-E30own")
		dataRS3 = myDataStore:GetAsync(player.UserId.."-RS3own")
		dataZ370 = myDataStore:GetAsync(player.UserId.."-Z370own")
		dataPUNTO = myDataStore:GetAsync(player.UserId.."-PUNTOown")
		dataTTRS = myDataStore:GetAsync(player.UserId.."-TTRSown")
		dataPOLO = myDataStore:GetAsync(player.UserId.."-POLOown")
		dataRC206 = myDataStore:GetAsync(player.UserId.."-RC206own")
		dataRLY206 = myDataStore:GetAsync(player.UserId.."-RLY206own")
		dataAUDIF1 = myDataStore:GetAsync(player.UserId.."-AUDIF1own")
	end)

  if success then
		credits.Value = creditdata
		JZA80own.Value = dataJZA80
		F40own.Value = dataF40
		FD7own.Value = dataFD7
		GT3RS991own.Value = dataGT3991RS
		E30own.Value = dataE30
		RS3own.Value = dataRS3
		Z370own.Value = dataZ370
		PUNTOown.Value = dataPUNTO
		TTRSown.Value = dataTTRS
		POLOown.Value = dataPOLO
		RC206own.Value = dataRC206
		RLY206own.Value = dataRLY206
		AUDIF1own.Value = dataAUDIF1
	else
		print("There was an error while getting your data")
		warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-credits",player.leaderstats.credits.Value)
		myDataStore:SetAsync(player.UserId.."-JZA80own",player.leaderstats.JZA80own.Value)
		myDataStore:SetAsync(player.UserId.."-F40own",player.leaderstats.F40own.Value)
		myDataStore:SetAsync(player.UserId.."-FD7own",player.leaderstats.FD7own.Value)
		myDataStore:SetAsync(player.UserId.."-GT3991RSown",player.leaderstats.GT3991RSown.Value)
		myDataStore:SetAsync(player.UserId.."-E30own",player.leaderstats.E30own.Value)
		myDataStore:SetAsync(player.UserId.."-RS3own",player.leaderstats.RS3own.Value)
		myDataStore:SetAsync(player.UserId.."-Z370own",player.leaderstats.Z370own.Value)
		myDataStore:SetAsync(player.UserId.."-PUNTOown",player.leaderstats.PUNTOown.Value)
		myDataStore:SetAsync(player.UserId.."-TTRSown",player.leaderstats.TTRSown.Value)
		myDataStore:SetAsync(player.UserId.."-POLOown",player.leaderstats.POLOown.Value)
		myDataStore:SetAsync(player.UserId.."-RC206own",player.leaderstats.RC206own.Value)
		myDataStore:SetAsync(player.UserId.."-RLY206own",player.leaderstats.RLY206own.Value)
		myDataStore:SetAsync(player.UserId.."-AudiF1own",player.leaderstats.AudiF1own.Value)
		-- myDataStore:SetAsync(player.UserId.."-credits",player.leaderstats.JZA80own.Value)
	end)
	
	if success then
		print(player.."'s Data Successfully Saved!")
	else
		print(player.."s Datas was an error when saving data")
		warn(errormessage)
	end
end)

The code is pretty immense, but I tried my best to explain this.

Thanks, vxcaie

5 Likes

I have changed the code a little bit, but I only made some uppercase value names lowercase.

2 Likes

I forgot to state that strangely only the credits get saved…

2 Likes

This is not how you want to store your data, this will use A TON of storage and will cause some data loss due to the amount of :SetAsync’s being called. I would save a table instead of new key for every car they could possibly own.

Example of setting data:

myDataStore:SetAsync(player.UserId, {
	"Car1",
	"Car2"
})

Example of getting data:

local carData = myDataStore:GetAsync(player.UserId)

for i, carName in ipairs(carData) do
	player:WaitForChild(carName).Value = true
end

If you need more assistance, don’t be afraid to ask.

2 Likes

How and where exactly should I write the car values?

2 Likes

I have rewrote your entire script for you, tell me what this prints:

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--//Variables
local myDataStore = DataStoreService:GetDataStore("myDataStore")

--//Tables
local carValues = {
	"JZA80own",
	"F40own",
	"FD7own",
	"GT3RS991own",
	"E30own",
	"RS3own",
	"Z370own",
	"PUNTOown",
	"TTRSown",
	"POLOown",
	"RC206own",
	"RLY206own",
	"AUDIF1own"
}

--//Functions
Players.PlayerAdded:Connect(function(player)
	local playerData = myDataStore:GetAsync(player.UserId) or {
		Credits = 0,
		Cars = {}
	}
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local credits = Instance.new("IntValue")
	credits.Name = "credits"
	credits.Value = playerData.Credits
	credits.Parent = leaderstats
	
	for i, valueName in ipairs(carValues) do
		local newValue = Instance.new("BoolValue")
		newValue.Name = valueName
		newValue.Value = playerData.Cars[valueName]
		newValue.Parent = leaderstats
		
		print(valueName)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local data = {
		Credits = player.leaderstats.credits.Value,
		Cars = {}
	}
	
	for i, child in ipairs(player.leaderstats:GetChildren()) do
		if table.find(carValues, child.Name) and child.Value then
			table.insert(data.Cars, child.Name)
		end
	end
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId, data)
	end)

	if success then
		print(player.."'s Data Successfully Saved!")
	else
		print(player.."s Datas was an error when saving data")
		warn(errormessage)
	end
end)

(you should probably clear your old data so it doesn’t conflict with the new saving method)

2 Likes

When joining the game, it just prints the names (obviously), when I try to buy a car and then rejoin it still forces me to rebuy the car.

1 Like

Do the BoolValues save? Could you also show me your buying script?

The values didn’t save, sadly.
The script. It’s quite immense, sadly. So to less stress you and not to waste your time, I’ll explain what the script does.

So when the person buying, if they attain the value, the car spawns. BUT, If they don’t, they have to buy the car with the chosen value. Then the car value turns to true.

game.ReplicatedStorage.Events.FQ400.OnServerEvent:Connect(function(chr)
	local fq400 = game.ReplicatedStorage.Database.LancerEvo8
	local spawned = fq400:Clone()
	spawned.Parent = game.Workspace
	if chr:IsInGroup(12020467) then
				spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.SupraMK4.OnServerEvent:Connect(function(chr)
	local supra4 = game.ReplicatedStorage.Database.SupraMK4
	local spawned = supra4:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.JZA80own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.JZA80own.Value == false and chr.leaderstats.credits.Value >= 35000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 35000
		chr.leaderstats.JZA80own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.F40.OnServerEvent:Connect(function(chr)
	local f458 = game.ReplicatedStorage.Database.F40
	local spawned = f458:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.F40own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.F40own.Value == false and chr.leaderstats.credits.Value >= 100000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 100000
		chr.leaderstats.F40own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
else
		print("Oops...")
		spawned:Destroy()
end
end)

game.ReplicatedStorage.Events.HondaCivicFD7.OnServerEvent:Connect(function(chr)
	local fn7 = game.ReplicatedStorage.Database.HondaCivicFD7
	local spawned = fn7:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.FD7own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.FD7own.Value == false and chr.leaderstats.credits.Value >= 15000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 15000
		chr.leaderstats.FD7own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.GT86.OnServerEvent:Connect(function(chr)
	local gt86 = game.ReplicatedStorage.Database.GT86
	local spawned = gt86:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.credits.Value >= 0 then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.GT3RS991.OnServerEvent:Connect(function(chr)
	local GT3RS1 = game.ReplicatedStorage.Database["991GT3RS"]
	local spawned = GT3RS1:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.GT3RS991own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.GT3RS991own.Value == false and chr.leaderstats.credits.Value >= 165000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 165000
		chr.leaderstats.GT3RS991own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.E30M3.OnServerEvent:Connect(function(chr)
	local E30M3 = game.ReplicatedStorage.Database.E30M3
	local spawned = E30M3:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.E30own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.E30own.Value == false and chr.leaderstats.credits.Value >= 75000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 750000
		chr.leaderstats.E30own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.RS3.OnServerEvent:Connect(function(chr)
	local RS3 = game.ReplicatedStorage.Database.RS3
	local spawned = RS3:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.RS3own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.RS3own.Value == false and chr.leaderstats.credits.Value >= 30000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 30000
		chr.leaderstats.RS3own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.Z370.OnServerEvent:Connect(function(chr)
	local Z370 = game.ReplicatedStorage.Database.Z370
	local spawned = Z370:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.Z370own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.Z370own.Value == false and chr.leaderstats.credits.Value >= 65000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 65000
		chr.leaderstats.Z370own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.Punto.OnServerEvent:Connect(function(chr)
	local Punto = game.ReplicatedStorage.Database.Punto
	local spawned = Punto:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.PUNTOown.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.PUNTOown.Value == false and chr.leaderstats.credits.Value >= 7950 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 7950
		chr.leaderstats.PUNTOown.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.TTRS.OnServerEvent:Connect(function(chr)
	local TTRS = game.ReplicatedStorage.Database.TTRS
	local spawned = TTRS:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.TTRSown.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.TTRSown.Value == false and chr.leaderstats.credits.Value >= 65000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 65000
		chr.leaderstats.TTRSown.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.PoloWRC.OnServerEvent:Connect(function(chr)
	local VWWRC = game.ReplicatedStorage.Database.PoloWRC
	local spawned = VWWRC:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.POLOown.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.POLOown.Value == false and chr.leaderstats.credits.Value >= 75000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 75000
		chr.leaderstats.POLOown.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.RC206.OnServerEvent:Connect(function(chr)
	local RC206 = game.ReplicatedStorage.Database.RC206
	local spawned = RC206:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.RC206own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.RC206own.Value == false and chr.leaderstats.credits.Value >= 12500 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 12500
		chr.leaderstats.RC206own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.RLY206.OnServerEvent:Connect(function(chr)
	local Rl206 = game.ReplicatedStorage.Database.RLY206
	local spawned = Rl206:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.RLY206own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.RLY206own.Value == false and chr.leaderstats.credits.Value >= 112500 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 112500
		chr.leaderstats.RLY206own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

game.ReplicatedStorage.Events.AudiF1.OnServerEvent:Connect(function(chr)
	local vagaudi1f = game.ReplicatedStorage.Database.AudiF1
	local spawned = vagaudi1f:Clone()
	spawned.Parent = game.Workspace
	if chr.leaderstats.AUDIF1own.Value == true then
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif chr.leaderstats.AUDIF1own.Value == false and chr.leaderstats.credits.Value >= 425000 then			
		chr.leaderstats.credits.Value = chr.leaderstats.credits.Value - 425000
		chr.leaderstats.AUDIF1own.Value = true
		spawned:SetPrimaryPartCFrame(chr.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

Do the BoolValues save their values though? That’s what I need to know.

1 Like

They do, thank you!
image
But the cars do not save… It’s all about the buying script now.

Could you show what the explorer looks like under the player? I want to make sure there aren’t any duplicate values.

Here you go.
image

Switch your buying script with this and tell me what it prints:

--//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
		print(player.Name, "owns", car.Name)
	else
		print(player.Name, "does not own", car.Name)
	end
	
	if value.Value then
		car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif player.leaderstats.credits.Value >= price then			
		player.leaderstats.credits.Value -= price
		
		value.Value = true
		car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		car:Destroy()
	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)
	local spawned = supra4:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.JZA80own.Value, 35000, spawned)
end)

ReplicatedStorage.Events.F40.OnServerEvent:Connect(function(player)
	local spawned = f458:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.F40own.Value, 100000, spawned)
end)

ReplicatedStorage.Events.HondaCivicFD7.OnServerEvent:Connect(function(player)
	local spawned = fn7:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.FD7own.Value, 15000, spawned)
end)

ReplicatedStorage.Events.GT86.OnServerEvent:Connect(function(player)
	local spawned = gt86:Clone()
	spawned.Parent = workspace
	
	if player.leaderstats.credits.Value >= 0 then
		spawned:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

ReplicatedStorage.Events.GT3RS991.OnServerEvent:Connect(function(player)
	local spawned = GT3RS1:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.GT3RS991own.Value, 165000, spawned)
end)

ReplicatedStorage.Events.E30M3.OnServerEvent:Connect(function(player)
	local spawned = E30M3:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.E30own.Value, 750000, spawned)
end)

ReplicatedStorage.Events.RS3.OnServerEvent:Connect(function(player)
	local spawned = RS3:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.RS3own.Value, 30000, spawned)
end)

ReplicatedStorage.Events.Z370.OnServerEvent:Connect(function(player)
	local spawned = Z370:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.Z370own.Value, 65000, spawned)
end)

ReplicatedStorage.Events.Punto.OnServerEvent:Connect(function(player)
	local spawned = Punto:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.PUNTOown.Value, 7950, spawned)
end)

ReplicatedStorage.Events.TTRS.OnServerEvent:Connect(function(player)
	local spawned = TTRS:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.TTRSown.Value, 65000, spawned)
end)

ReplicatedStorage.Events.PoloWRC.OnServerEvent:Connect(function(player)
	local spawned = VWWRC:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.POLOown.Value, 75000, spawned)
end)

ReplicatedStorage.Events.RC206.OnServerEvent:Connect(function(player)
	local spawned = RC206:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.RC206own.Value, 12500, spawned)
end)

ReplicatedStorage.Events.RLY206.OnServerEvent:Connect(function(player)
	local spawned = Rl206:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.RLY206own.Value, 112500, spawned)
end)

ReplicatedStorage.Events.AudiF1.OnServerEvent:Connect(function(player)
	local spawned = vagaudi1f:Clone()
	spawned.Parent = workspace
	
	ValidateSpawn(player.leaderstats.AUDIF1own.Value, 425000, spawned)
end)

Made a typo, try it again.

2 Likes

I made a typo @vxcaie, try it again.

3 Likes

It prints the car values, but it also prints this:

Alright, try this:

--//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
		print(player.Name, "owns", car.Name)
	else
		print(player.Name, "does not own", car.Name)
	end

	if value.Value then
		car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	elseif player.leaderstats.credits.Value >= price then			
		player.leaderstats.credits.Value -= price

		value.Value = true
		car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		car:Destroy()
	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)
	local spawned = supra4:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.JZA80own, 35000, spawned)
end)

ReplicatedStorage.Events.F40.OnServerEvent:Connect(function(player)
	local spawned = f458:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.F40own, 100000, spawned)
end)

ReplicatedStorage.Events.HondaCivicFD7.OnServerEvent:Connect(function(player)
	local spawned = fn7:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.FD7own, 15000, spawned)
end)

ReplicatedStorage.Events.GT86.OnServerEvent:Connect(function(player)
	local spawned = gt86:Clone()
	spawned.Parent = workspace

	if player.leaderstats.credits.Value >= 0 then
		spawned:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, -1.25, 0))
	else
		print("Oops...")
		spawned:Destroy()
	end
end)

ReplicatedStorage.Events.GT3RS991.OnServerEvent:Connect(function(player)
	local spawned = GT3RS1:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.GT3RS991own, 165000, spawned)
end)

ReplicatedStorage.Events.E30M3.OnServerEvent:Connect(function(player)
	local spawned = E30M3:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.E30own, 750000, spawned)
end)

ReplicatedStorage.Events.RS3.OnServerEvent:Connect(function(player)
	local spawned = RS3:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.RS3own, 30000, spawned)
end)

ReplicatedStorage.Events.Z370.OnServerEvent:Connect(function(player)
	local spawned = Z370:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.Z370own, 65000, spawned)
end)

ReplicatedStorage.Events.Punto.OnServerEvent:Connect(function(player)
	local spawned = Punto:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.PUNTOown, 7950, spawned)
end)

ReplicatedStorage.Events.TTRS.OnServerEvent:Connect(function(player)
	local spawned = TTRS:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.TTRSown, 65000, spawned)
end)

ReplicatedStorage.Events.PoloWRC.OnServerEvent:Connect(function(player)
	local spawned = VWWRC:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.POLOown, 75000, spawned)
end)

ReplicatedStorage.Events.RC206.OnServerEvent:Connect(function(player)
	local spawned = RC206:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.RC206own, 12500, spawned)
end)

ReplicatedStorage.Events.RLY206.OnServerEvent:Connect(function(player)
	local spawned = Rl206:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.RLY206own, 112500, spawned)
end)

ReplicatedStorage.Events.AudiF1.OnServerEvent:Connect(function(player)
	local spawned = vagaudi1f:Clone()
	spawned.Parent = workspace

	ValidateSpawn(player.leaderstats.AUDIF1own, 425000, spawned)
end)

I’m still wondering how the value is true, but then the script reads the value as false.

3 Likes

It still forces me to rebuy the car when rejoining, sadly.

2 Likes

For your saving script, I forgot to implement :BindToClose which fires when the server shutdowns.

Code:

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--//Variables
local myDataStore = DataStoreService:GetDataStore("myDataStore")

--//Tables
local carValues = {
	"JZA80own",
	"F40own",
	"FD7own",
	"GT3RS991own",
	"E30own",
	"RS3own",
	"Z370own",
	"PUNTOown",
	"TTRSown",
	"POLOown",
	"RC206own",
	"RLY206own",
	"AUDIF1own"
}

--//Functions
local function SaveData(player)
	local data = {
		Credits = player.leaderstats.credits.Value,
		Cars = {}
	}

	for i, child in ipairs(player.leaderstats:GetChildren()) do
		if table.find(carValues, child.Name) and child.Value then
			table.insert(data.Cars, child.Name)
		end
	end
	
	local success, errorMessage = pcall(function()
		myDataStore:SetAsync(player.UserId, data)
	end)
	
	if not success then
		warn(errorMessage)
	end
end

Players.PlayerAdded:Connect(function(player)
	local playerData = myDataStore:GetAsync(player.UserId) or {
		Credits = 0,
		Cars = {}
	}

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

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

	for i, valueName in ipairs(carValues) do
		local newValue = Instance.new("BoolValue")
		newValue.Name = valueName
		newValue.Value = playerData.Cars[valueName]
		newValue.Parent = leaderstats
	end
end)

Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(SaveData, player)
	end
end)
1 Like

What BindToClose exactly does? I read about people saying it but I didn’t understand why it’s used.
I’ll edit this reply shortly when I’ll test the code.

Result: It has the same issues, but it prints this: