Can't get for loop to ignore deleting certain parts despite parts being in an "IgnoreList"

Hello! I’m really a bit stumped by a seemingly simple (probably is) issue with a Parts/Model deleter I’m making.

Basically, I’m working on a setting in a game I’m working on that locally deletes a bunch of things inside of a building because it’s a little performance taxing on older machines.

Snippet of code I can’t get working:

local upgradesToIgnore = {"GFWalls", "FirstFloor", "FFWalls", "SecondFloor", "Door", "DontDelete"}

for i, v in pairs(ylw:GetDescendants()) do
	if upgradesToIgnore[v.Name] == nil then
		v:Destroy()
	end
rnSVC.RenderStepped:Wait()
end

Now, what I think this should be doing is checking if the part’s name it’s found is in the “upgradesToIgnore” table and if the parts name is nil (i.e not in the table, thus returning nil) then it should skip that part and do the next for loop iteration.

Thing is, it deletes all the parts anyway, what am I doing wrong here?

Cheers, reddust1

1 Like

You are storing the names in an array, not a dictionary. Meaning when you check if table[part.Name] it will always be nil as it is a numbered list.

You need to store the names in a dictionary like so

local ignore = {"name" = true}

Or, you would have to loop thru the array you have now and compare the values. Which is less efficient.

2 Likes
local upgradesToIgnore = {["GFWalls"] = "GFWalls", ["FirstFloor"] = "FirstFloor", ["FFWalls"] = "FFWalls", ["SecondFloor"] = "SecondFloor", ["Door"] = "Door", ["DontDelete"] = "DontDelete"}

for i, v in pairs(ylw:GetDescendants()) do
	if upgradesToIgnore[v.Name] ~= nil and upgradesToIgnore[v.Name] ~= v.Name then
		v:Destroy()
	end
	rnSVC.RenderStepped:Wait()
end

Made your change to the ignorelist and updated the for loop to work accordingly, but I still have the same problem. Have I done the dictionary correctly? I haven’t really worked much with dictionaries like this so I’m fairly clueless.

Thanks for pointing out the incorrect format though, I should’ve spotted this as I use arrays fairly frequently in other things (efficiently, do not fear)

EDIT: Noticed a mistake I made, updated code (still doesn’t work tho, now it deletes nothing)

local upgradesToIgnore = {["GFWalls"] = "GFWalls", ["FirstFloor"] = "FirstFloor", ["FFWalls"] = "FFWalls", ["SecondFloor"] = "SecondFloor", ["Door"] = "Door", ["DontDelete"] = "DontDelete"}

for i, v in pairs(ylw:GetDescendants()) do
	if not upgradesToIgnore[v.Name] then
		v:Destroy()
	end
	rnSVC.RenderStepped:Wait()
end

This still deletes what’s in the ignore list.

This check will always fail:

upgradesToIgnore[v.Name] ~= v.Name

In the first check you check if the string is a key in the dictionary, which it may or not be. If it is, the next check asks if the value that belongs to the key is not equal to the key itself, but in your case the key and value are always the same.

You can simply delete that check and only keep this part:

upgradesToIgnore[v.Name] ~= nil

You just want to know if the key exists in the table. What its value is does not matter. Usually I would just set the value to true as suggested in the first reply.

1 Like

I changed it to just the simple check you suggested and also set all the values in the dictionary back to true but it still deletes the parts I don’t want it to.

The parts in workspace are definitely called what I put in the ignore list.

Are you 100% sure you copy and pasted the code I provided? It seems to work for me.

Yes I definitely did copy and paste the code, I did it again to be sure but to no avail.

This didn’t work, it also appears you’re only running this for loop for the ignored parts but not all the others?