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.
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.
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)
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.