How to save values into a table?

  1. What do you want to achieve? Save values with a table so they don’t take so much to load

  2. What is the issue? I don’t know how how to save them in a table

  3. What solutions have you tried so far? Yeah I tried to search for tutorials and I still can’t figure out how to make it

I’m not asking you to write the entire script for me! Just modify the values to save into a table so I can understand better how it works and follow that model for the next values I’m gonna save! Thank you! :slight_smile:

-- local players = game:GetService("Players")
local dataStore = game:GetService("DataStoreService")
local CarDataStore1 = dataStore:GetDataStore("VehicleDataStore0")

players.PlayerAdded:Connect(function(player)

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


	local car1 = Instance.new("BoolValue")
	car1.Name = "2020BloxWagenGulf"
	car1.Parent = player.OwnedCars
	car1.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car1.Value)
	
	local car2 = Instance.new("BoolValue")
	car2.Name = "1983PIATDOS"
	car2.Parent = player.OwnedCars
	car2.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car2.Value)
	
	local car3 = Instance.new("BoolValue")
	car3.Name = "2020TacoCarry"
	car3.Parent = player.OwnedCars
	car3.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car3.Value)
	
	local car4 = Instance.new("BoolValue")
	car4.Name = "2015PiatHundred"
	car4.Parent = player.OwnedCars
	car4.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car4.Value)
	
	local car5 = Instance.new("BoolValue")
	car5.Name = "2003JacuzziRS4"
	car5.Parent = player.OwnedCars
	car5.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car5.Value)
	
	local car6 = Instance.new("BoolValue")
	car6.Name = "2020TokenModelIII(Uncustomizable)"
	car6.Parent = player.OwnedCars
	car6.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car6.Value)
	
	local car7 = Instance.new("BoolValue")
	car7.Name = "1997ToySupra"
	car7.Parent = player.OwnedCars
	car7.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car7.Value)
	
	local car8 = Instance.new("BoolValue")
	car8.Name = "2008BAM320Y"
	car8.Parent = player.OwnedCars
	car8.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car8.Value)
	
	---
	
	car1.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car1.Value)
	car2.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car2.Value)
	car3.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car3.Value)
	car4.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car4.Value)
	car5.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car5.Value)
	car6.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car6.Value)
	car7.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car7.Value)
	car8.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car8.Value)
						
end)
	end)
		end)
			end)
				end)
				end)
				end)
				end)
				end)
1 Like

From what I know you have to use DataStores to do that

1 Like

I’m already doing that
local dataStore = game:GetService(“DataStoreService”)

Okay hang on, this is messy so first off creating that much variables with how many there are can be compressed for easier usage. Almost done rewriting.

1 Like

Ok, I don’t have any idea of how to use them, sorry, but have you tried searching in the Documentation?

Also another thing, please put all of that into a table, it’s just hurting my eyes to see all of that variables repeated over and over and over again

Thats what I want to do but I don’t know how…

Feel free to move the table of cars into a Folder or set attributes for each car.

Optimized Approach

local dataStore = game:GetService("DataStoreService")
local CarDataStore1 = dataStore:GetDataStore("VehicleDataStore0")

-- Define the cars you want to save
local cars = {
    {
        Name = "2020BloxWagenGulf",
        Value = false,
    },
    {
        Name = "1983PIATDOS",
        Value = false,
    },
    {
        Name = "2020TacoCarry",
        Value = false,
    },
    {
        Name = "2015PiatHundred",
        Value = false,
    },
    {
        Name = "2003JacuzziRS4",
        Value = false,
    },
    {
        Name = "2020TokenModelIII(Uncustomizable)",
        Value = false,
    },
    {
        Name = "1997ToySupra",
        Value = false,
    },
    {
        Name = "2008BAM320Y",
        Value = false,
    },
}

-- Connect to the player added event
game.Players.PlayerAdded:Connect(function(player)
    -- Create a table to store the player's car data
    local playerCars = {}

    -- Create a folder to hold the player's owned cars
    local folder = Instance.new("Folder")
    folder.Name = "OwnedCars"
    folder.Parent = player

    -- Create a BoolValue for each car and add it to the playerCars table
    for i, carData in ipairs(cars) do
        local car = Instance.new("BoolValue")
        car.Name = carData.Name
        car.Parent = player.OwnedCars
        car.Value = CarDataStore1:GetAsync(player.UserId) or false
        playerCars[i] = {
            Car = car,
            Data = carData,
        }
    end

    -- Connect to the player leaving event to save their car data to the datastore
    player.CharacterRemoving:Connect(function()
        for _, carData in ipairs(playerCars) do
            CarDataStore1:SetAsync(player.UserId.."_"..carData.Data.Name, carData.Car.Value)
        end
    end)

    -- Load the player's car data from the datastore
    local carData = CarDataStore1:GetAsync(player.UserId)
    if carData then
        for i, carInfo in ipairs(playerCars) do
            local value = carData[carInfo.Data.Name]
            if value ~= nil then
                carInfo.Car.Value = value
            end
        end
    end

    -- Connect to the server shutting down event to save the player's car data to the datastore
    game:BindToClose(function()
        for _, carData in ipairs(playerCars) do
            CarDataStore1:SetAsync(player.UserId.."_"..carData.Data.Name, carData.Car.Value)
        end
    end)
end)
1 Like
for _,v in player.OwnedCars:GetChildren() do
    if v:IsA("BoolValue") then
       table.insert(table,v)
    end
end

Then you just save them through iterating through the table.

@corehimself solved the problem, that’s everything you need I think

1 Like

Thank you so much it works! That was everything I literally needed!

Here’s a rewrite, this allows you to easily add entries for each car, versus the old version.

I’ve implemented the following changes:

  • Using a loop instead of repeating code for each car
  • Using a table to store car data instead of separate variables
  • Only update the datastore when necessary
  • Easy table modification
local dataStore = game:GetService("DataStoreService")
local CarDataStore1 = dataStore:GetDataStore("VehicleDataStore0")

local defaultCarOwnership = {
	["2020BloxWagenGulf"] = false,
	["1983PIATDOS"] = false,
	["2020TacoCarry"] = false,
	["2015PiatHundred"] = false,
	["2003JacuzziRS4"] = false,
	["2020TokenModelIII(Uncustomizable)"] = false,
	["1997ToySupra"] = false,
	["2008BAM320Y"] = false,
}

function setCarOwnership(player, carName, ownership)
	local folder = player:FindFirstChild("OwnedCars")
	if not folder then
		folder = Instance.new("Folder")
		folder.Name = "OwnedCars"
		folder.Parent = player
	end

	local car = folder:FindFirstChild(carName)
	if not car then
		car = Instance.new("BoolValue")
		car.Name = carName
		car.Parent = folder
	end

	car.Value = ownership
	CarDataStore1:SetAsync(player.UserId .. "_" .. carName, ownership)
end

function getCarOwnership(player, carName)
	local folder = player:FindFirstChild("OwnedCars")
	if not folder then
		return defaultCarOwnership[carName]
	end

	local car = folder:FindFirstChild(carName)
	if not car then
		return defaultCarOwnership[carName]
	end

	local value = CarDataStore1:GetAsync(player.UserId .. "_" .. carName)
	if value ~= nil then
		return value
	else
		return car.Value
	end
end

players.PlayerAdded:Connect(function(player)
	for carName, defaultOwnership in pairs(defaultCarOwnership) do
		local ownership = getCarOwnership(player, carName)
		setCarOwnership(player, carName, ownership)
	end
end)

for _, player in ipairs(players:GetPlayers()) do
	for carName, defaultOwnership in pairs(defaultCarOwnership) do
		local ownership = getCarOwnership(player, carName)
		setCarOwnership(player, carName, ownership)
	end
end

players.PlayerRemoving:Connect(function(player)
	CarDataStore1:RemoveAsync(player.UserId)
end)

game:BindToClose(function()
	for _, player in ipairs(players:GetPlayers()) do
		CarDataStore1:SetAsync(player.UserId, nil)
	end
end)
1 Like

Keep in mind, it’s better to set the instance parent right after you set it’s changes. This is to prevent any lags. PSA: Don't use Instance.new() with parent argument

3 Likes

For some reason when I rejoin the values don’t save

2 Likes

Make sure the datastore keys are correct.

Check the output of datastore:GetDataStore("VehicleDataStore0"), if it returns nil there’s an issue with your datastore.

Check the output of CarDataStore1:SetAsync(player.UserId .. "_" .. carName, ownership)

Check that the datastore values are being retrieved correctly. You can do this by checking the output of CarDataStore1:GetAsync(player.UserId .. "_" .. carName) to see if it returns the expected value.

If you have checked all of these things and are still having issues with the data not being saved correctly, there may be an issue with the DataStoreService or with the way you are using it.

Also you forgot to put local players = game:GetService(“Players”)
Because of this the ownedcars folder wasn’t there

If I reset my character the values are still there but when I close the game and then rejoin then the values are not enabled anymore

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