Invalid argument #1 to 'insert' (table expected, got string)

local Table = {"Cat", "Dog"}

local function UpdateEquip()
	for _, Pets in pairs(Pet:GetChildren()) do
		Pets:GetPropertyChangedSignal("Value"):Connect(function()
			if Pets.Value == true then
				print(Pets)
				table.insert(Table, Player:FindFirstChild(Pets.Name))
			elseif Pets.Value == false then
				print(Pets)
				table.remove(Table, Player:FindFirstChild(Pets.Name))
			end
		end)
	end
end

Every variable works as intended but as the title states that I cannot insert a Pet to the table when property is changed. Any ideas?

4 Likes

Are you redefining Table somewhere else in the code?

1 Like

Yes it is using the Datastore2 module with the :Get function. Does that affect the code at all?

I’m not sure. Could you try printing Table when the value changes?

Print out the table with a loop, if it’s a table, it should print out

dog
cat

Weird… It only prints out Cat even when the property value changed for both the Cat and Dog. There is something wwrong about my code probably unrelated to the post

Could I see the code where you save the player’s data? I would assume the problem lies there.

local EquippedDataStore = DataStoreModule("Equipped", Player)

local function UpdateEquip(Value)
		Table = EquippedDataStore:Get(Value)
		for _, Pets in pairs(Pet:GetChildren()) do

UpdateEquip(Table)

EquippedDataStore:OnUpdate(UpdateEquip)
1 Like

Well, I asked for the data saving code, but one problem would be

You send over the table, but try to get the data of the table…?

local function UpdateEquip(Table)
	for _, Pets in pairs(Pet:GetChildren()) do
		Pets:GetPropertyChangedSignal("Value"):Connect(function()
			if Pets.Value == true then
				print(Pets)
				table.insert(Table, Player:FindFirstChild(Pets.Name))
			elseif Pets.Value == false then
				print(Pets)
				table.remove(Table, Player:FindFirstChild(Pets.Name))
			end
		end)
	end
end

UpdateEquip(Table)
EquippedDataStore:OnUpdate(UpdateEquip)

I’m not sure how OnUpdate works with ds2 (I don’t use ds2), but I believe it will pass the table as well.

4 Likes

That is how data saving works in Datastore2 I believe. I think what I did was getting the default value of the table and adding onto the table when value changes. I’m not saving the table yet because it errors.

Can you tell me what you mean by this
You send over the table, but try to get the data of the table?

Yes, I actually just searched that up.

local function UpdateEquip(Table)
	for _, Pets in pairs(Pet:GetChildren()) do
		Pets:GetPropertyChangedSignal("Value"):Connect(function()
			if Pets.Value == true then
				print(Pets)
				table.insert(Table, Player:FindFirstChild(Pets.Name))
			elseif Pets.Value == false then
				print(Pets)
				table.remove(Table, Player:FindFirstChild(Pets.Name))
			end
		end)
	end
end

local function UpdatedEquipped(Value)
	UpdateEquip(EquippedDataStore:Get(Value))
end

UpdateEquip(Table)
EquippedDataStore:OnUpdate(UpdatedEquipped)
4 Likes

I am fairly new to scripting and I am practicing my skills so I may not know a lot of the terminology or how things work.

I also don’t work with ds2 much, but the main problem was that you were trying to get a saved value from the original table. Another problem might be overlapping connections.

3 Likes

Yes thank you that was the solution!

1 Like

Thank you again for your patience