DataStore2 table is nil

  • What are you attempting to achieve? (Keep it simple and clear)

    I’m trying to check if the car is owned by the Player(If it’s in the owned table in the DataStore)

  • What is the issue? (Keep it simple and clear - Include screenshots/videos/GIFs if possible)

    If i try to get the table after setting it then it returns nil. I set it on line 21 in ‘OnCarPurchase
    Video

  • What solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)

    I don’t remember

v–DataStore2Script–v

local DataStore2 = require(1936396537)

local defaultvalue = {}

local OwnedCars

game.Players.PlayerAdded:Connect(function(plr)
	
	local carDataStore = DataStore2("OwnedCars", plr)
	
	
	
	local function carsUpdate(updatedValue) -- updatedValue = default value OR passed in by OnUpdate
		
		OwnedCars = carDataStore:Get(updatedValue)
		
	end
	
	carsUpdate(defaultvalue)
	
	
	
	carDataStore:OnUpdate(carsUpdate)
	
end)

v–OnCarPurchase–v

game.ReplicatedStorage.PurchaseCar.OnServerEvent:Connect(function(Player, CarModelName)
	local carDataStore = DataStore2("OwnedCars", Player)
	if CarsAPI.Cars[CarModelName] then
		print(carDataStore:Get())
		if typeof(carDataStore:Get()) == "table" then
			local OwnedCars = carDataStore:Get()
			if OwnedCars[CarModelName] then
				warn("Player has car")
			elseif not OwnedCars[CarModelName] then
				if Money > CarsAPI.Cars[CarModelName].Price then
					print("Has enough money to buy")
					local TableWithAddedCar = table.insert(OwnedCars, #OwnedCars + 1, CarModelName)
					carDataStore:Set(TableWithAddedCar)
					print(Player.Name.." Purchased "..CarModelName)
				else
					warn(Player.Name.." Doesn't have enough money to buy "..CarModelName)
				end
			end
			
		end
		
	end
end)
1 Like

The reason why you’re getting a nil value is most likely because you’re setting a function to the value of the data.

Additionally, I would like to point out that you don’t need to add #OwnedCars + 1 to your code as it is automatically done for you. You can read more about this here.

As I see that the code is checking if the index of the table exists, you should set OwnedCars[CarModelName] = true instead of table.insert.

Replace your code with this and let me know what happens.

game.ReplicatedStorage.PurchaseCar.OnServerEvent:Connect(function(Player, CarModelName)
	local carDataStore = DataStore2("OwnedCars", Player)
	if CarsAPI.Cars[CarModelName] then
		print(carDataStore:Get())
		if typeof(carDataStore:Get()) == "table" then
			local OwnedCars = carDataStore:Get()
			if OwnedCars[CarModelName] then
				warn("Player has car")
			elseif not OwnedCars[CarModelName] then
				if Money > CarsAPI.Cars[CarModelName].Price then
					print("Has enough money to buy")
					OwnedCars[CarModelName] = true
					carDataStore:Set(OwnedCars)
					print(Player.Name.." Purchased "..CarModelName)
				else
					warn(Player.Name.." Doesn't have enough money to buy "..CarModelName)
				end
			end
			
		end
		
	end
end)
3 Likes

Thank you, i didn’t even think about that :slight_smile: Will test when i get on my computer.

You’re welcome. Keep me updated on the status of this issue.

It works kinda. Now my problem is it runs the code for elseif notOwnedCars[CarModelName] then even after the table has been set

Nvm i fixed it with this code instead of checking the way i did before

local m = {}

function m.FindTableOccurrence(Haystack, Needle)
	-- Returns one occurrence of `Needle` in `Haystack`

	-- Search for the first instance of `Needle` found and return it
	for Index, Value in pairs(Haystack) do
		if Value == Needle then
			return Index;
		end;
	end;

	-- If no occurrences exist, return `nil`
	return nil;

end;

return m