Illogical error[for me]

function:

local function showSelected()
	local partsToCheck = {}
	partsToCheck = selectedParts
	for i, selectionBox in ipairs(folderSelectionBoxes:GetChildren()) do 
		if table.find(selectedParts,selectionBox.Adornee) == nil then
			selectionBox:Destroy()
		else
			local elementNumber = table.find(partsToCheck,selectionBox.Adornee)
			print(#selectedParts)
			local tt = {"a","a","a","a","a","a","a","a"}
			table.remove(partsToCheck,tonumber(elementNumber))-- --This 
			print(#selectedParts)
		end
	end
	
	for i, part in ipairs(partsToCheck) do 
		local t = createSelectionBox(part,Color3.new(0.196078, 0.592157, 0.992157),0,folderSelectionBoxes)
	end
	
end

I really have tested everything, from time-shifted to other tables, I haven’t the slightest idea why this is the case:
The first print always has one more than the second, even though the tables hardly have anything to do with each other.
With the pointless tt table, which is only there for testing, it works.
Please please please help me, I’m desperate, I’ve been sitting on this f*cking problem for 2 hours.

2 Likes
  • You print #selectedParts
  • You remove one element from the table
  • You print #selectedParts again

Why are you confused? This is expected behavior when removing elements from a table.

1 Like

I do not remove anything from selected parts. I remove from partsToCheck.

1 Like

The reason is from this line. You are in fact printing from the same table you just set it to a different variable.

What you are likely trying to do is copy the table so that “partsToCheck” is it’s own table that includes the parts from selectedParts.

You can use table.clone to clone the table

partsToCheck = table.clone(selectedParts)

This will error if selectedParts = nil so you can add this to make it return an empty table

partsToCheck = table.clone(selectedParts or {})

This says “clone selectedParts or this empty table if selectedParts is nil”.

2 Likes

The first print, prints that what it should. The second print is wrong. I was able to contain it so that I know exactly that it comes from the line. This is changed by -1 exactly in the line. The one from the table I don’t change anything.

  1. Print [e.g]:
    2 [good]
  2. Print:
    1 [bad]
    It gets changed here:
table.remove(partsToCheck,tonumber(elementNumber))

I’ll try it and tell you the result.
THANK YOU!
It works, but why?

I mean there:

	local partsToCheck = {}
	partsToCheck = selectedParts
	for i, selectionBox in ipairs(folderSelectionBoxes:GetChildren()) do 
		if table.find(selectedParts,selectionBox.Adornee) == nil then
			selectionBox:Destroy()
		else
			local elementNumber = table.find(partsToCheck,selectionBox.Adornee)
			print(#selectedParts)

It should be the same, but it gets changed here
table.remove(partsToCheck,tonumber(elementNumber))-- --This
I mean I change the other table there and don’t set them to the same there again?
print(#selectedParts)

1 Like

When you set a variable to a table, the variable becomes a reference to the table.

local t = {1,2,3}

local var = t 
-- "var" is referencing t, if you change t then var is also changed because var *is* t

table.insert(t,4)

print(t) --- 1,2,3,4
print(var)  -- 1,2,3,4


-- var and t reference the exact same table

This is not the case though for strings, numbers, booleans, nils

local str = "Hello World"

local var = str 
-- Here var is *not* a reference to str, it is the string "Hello World"
-- Changing str does not change var

str = "different string"

print(str) -- "different string"
print(var) -- "Hello World"

There are 8 types in lua, 4 of have reference functionality and 4 of them have direct functionality.

  • Reference: table, function, thread, userdata
  • Direct: string, number, boolean, nil
2 Likes

Huh, thanks for the info, I didn’t know that, I thought it was like with string , number , boolean , nil. Thank you!

2 Likes

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