table.Insert() for DataStore2?

Hi! I am making a pet shop system and I am using DataStore2 and altered my original design instead of adding bools for every pet I am using a list. However… table.Insert() doesn’t work. Or maybe it does and I am just a noob scripter. :stuck_out_tongue:

The Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStore2 = require(1936396537)

local remoteEvent = ReplicatedStorage.petShopEvent
local petInfo = require(ReplicatedStorage.PetInfo)

remoteEvent.OnServerEvent:Connect(function(player, node)
  local coinsStore = DataStore2("coins", player)
local ownedPets = DataStore2("pets", player)
local petTable = ownedPets:GetTable({})

local price = petInfo["pet"..node].price
local selectedPet = "pet"..node

if petTable.selectedPet == nil then -- Not owned yet.
	if coinsStore:Get(0) > price then
		-- The player has enough money
		print("The player has enough money")
		petTable.Insert(selectedPet)
		ownedPets:Set(petTable)
	else
		warn("Not enough money")
	end
else
	warn("Already owned")
end

end)

The error: image

table.insert(petTable, number, val)

This should work unless DataStore2 has a built in function for inserting values into tables.

1 Like

Wait, do I use table.insert? or Do I use my own table? or do I put the table in the parameters? if so where do I add the string that I wanted in the table in the first place?

For example you could do something like this (not edited for DataStore2):

local tab = {}

table.insert(tab, 1, "hi)

print(tab) --> "hi"

Ok, by number do you mean the index number?

Yes. The index number is where it will be placed in that table.

In this case the index is the number pointing to a location in the table.

1 Like

Alright thank you so much! Helps a lot.

1 Like

Glad it could help. ExcessEnergy explained the index number much better than I did. :joy:

1 Like