Issue with Color Saving

Hello. So I am trying to save Car Color, so when purchasing car and joining the game again it doesn’t load color or I don’t save it right, I don’t know. It prints out “nil”.

This is code here:

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("6")
local CarListModule = require(game.ServerScriptService.VehiclesList)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local function DeductMoney(player, price)
	local money = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Money")
	if money then
		if money.Value >= price then
			money.Value = money.Value - price
			return true  
			warn("Player doesn't have enough money.")
			return false  
		warn("Player's money data not found.")
		return false  

function PurchaseCar(player, carId, VehicleColor)
	local PlayerOwnedCarsFolder = ServerStorage.PurchasedVehicles:WaitForChild(player.UserId)
	local Vehicles = ServerStorage.Vehicles

	local carInfo = CarListModule.GetCarInfo(carId)
	if not carInfo then
		return false, "Invalid car ID"

	if PlayerOwnedCarsFolder:FindFirstChild(carId) then
		return false, print("Player own's Vehicle")

	local success, err = DeductMoney(player, carInfo.Price)
	if not success then
		return false, err

	local playerKey = "Player_" .. player.UserId
	local playerData = PlayerDataStore:GetAsync(playerKey) or {}
	playerData.PurchasedCars = playerData.PurchasedCars or {}
	table.insert(playerData.PurchasedCars, carId)
	playerData.PurchasedCars[carId] = VehicleColor

	PlayerDataStore:SetAsync(playerKey, playerData)
	print("Player Purchased Car",carInfo.Name)

	local carToClone = ServerStorage.Vehicles:FindFirstChild(carId)
	if carToClone then
		carToClone:Clone().Parent = PlayerOwnedCarsFolder
		warn("No vehicle found")

	return true, "Car purchased successfully"

function LoadPurchasedCars(player)
	local playerKey = "Player_" .. player.UserId
	local playerData = PlayerDataStore:GetAsync(playerKey)

	local Vehicles = ServerStorage.Vehicles
	local PlayersOwnedFolder = ServerStorage.PurchasedVehicles:WaitForChild(player.UserId)

	if playerData and playerData.PurchasedCars then
		for _, carId in ipairs(playerData.PurchasedCars) do
			print("Loaded: "..carId)
			local car = Vehicles:FindFirstChild(carId):Clone()
			car.Parent = PlayersOwnedFolder

			local ColorData = playerData.PurchasedCars[carId]
			if ColorData then
				local VehicleColor = ColorData.VehicleColor
				print("Color:", VehicleColor)
				print("Color data not found for car:", carId)
		print("Player data or purchased cars not found for player:", player.Name)

	if player then

ReplicatedStorage.CarDealershipEvent.PurchaseEvent.OnServerEvent:Connect(function(player, VehicleName)
	PurchaseCar(player, VehicleName, "White") --This is just for test

1 Like

Are you saving each individual element of the colour? If it is a Color3 value, you have to save each part on its own.

local myColour = Color3.fromRGB(85, 170, 0)
local colourSaveData = {
    ["Red"] = myColour.R,
    ["Green"] = myColour.G,
    ["Blue"] = myColour.B

--then, to reassemble it:
local assembledColour = Color3.fromRGB(colourSaveData["Red"], colourSaveData["Green"], colourSaveData["Blue"])

Always use a pcall() when accessing the DataStore. It’s a network request, so it can fail. Not using a pcall() means that you can’t handle any errors and thread execution will halt if errors are thrown, which could lead to some problems in your script.

1 Like

For this one, I am using BrickColor, just for testing. I will add pcall() to my script.

1 Like

It may be easier to just make a list that goes to a color from a number. Then you just need to save/work with the number.

1 = red
2 = blue

exc …
Depends on if you do have a number of set colors.

Oh, I see, so Instead of white,blue,red I use numbers for paint.

Makes it pretty easy to store the data. After you read the number convert that to your color.
In fact you could do everything that way. Car has 2 digits color has 2 digits.

1217 … would be car 12 color 17 or 12 17 for two saved data. I use something like this for one of my games. 12175000 … car 12 color 17 cash 5000, all in one data save.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.