Saving cars as strings in datastore [SOLVED BY MYSELF]

Hello, I need help saving car names in strings but in a table like shown in the picture below.

I have a folder called Player_Cars / Car folder and in there cars get put in when they buy them but when the player leaves they won’t get loaded in

table

My code doesn’t work and I’m not sure why. I get no error messages.

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")





local function onPlayerJoin(player)  -- Runs when players join

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

	local ownedCars
	



	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player
		ownedCars = data.ownedCars
		
		if ownedCars ~= nil then
			
		
		for i, v in pairs(ownedCars) do
			
			local carClone = game.ReplicatedStorage.Cars[i]:Clone()
			carClone.Parent = player.Player_Cars
			
		end
	end

	else

		-- Data store is working, but no current data for this player
	ownedCars = nil
		

	end

end

local function saveCars(player)
	
	local playerCars = {}
	
	for i, v in pairs(player.Player_Cars:GetChildren()) do
		
		table.insert(playerCars, v.Name)
		
	end
	
	return playerCars
	
end


local function onPlayerExit(player)  --Runs when players exit

local ownedCars = saveCars(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, ownedCars) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

This is a table of one item, a string that is all three words separated by commas, so if you print the table you get 1 string item (volvo, saab, volkswagen). Don’t you mean {"volvo", "saab", "volkswagen"} to get 3 different strings?

Also, be very careful using actual names of vehicles. If your game gets slightly popular you’re going to get DMCA’d for copyrighted material. My game wasn’t popular and the name Formula One (which is copyrighted) got it shut down. That’s why popular games use names that are close to the names of real items.

I’m not too familiar with datastores though.

1 Like

That’s just an example. If you look at the actual code you’ll understand that that’s not the case! But thanks.