I,v in pairs loop filter doesn't work

Hey, first of all thanks for taking your time and reading my problem. Let’s get straight to the point though, I am trying to filter my character out this i,v in pairs loop. Meaning, when a part is a descendant of my character. It shouldn’t be added to the table, yet it still gets added to the table and I have no idea why.

Code:

for _,v in pairs(game.Workspace:GetDescendants()) do
	if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("BasePart") then
		if v.Name ~= "Baseplate" and v.CanCollide == true then
			if not v:IsDescendantOf(character) then
				print(v)
				table.insert(table_1,v)	
			end
		
		end
	end
end

table.insert() format should be table.insert(table,index,value)

You don’t need to specify the index parameter since if you only pass 2 arguments then 2nd argument will count as the value that is going to be inserted and index will be the length of the table plus 1.

1 Like

I didn’t know that, thanks

there’s no problem with the table.insert, it’s the filter.

What’s the code you used to define character, and when does this loop run?

Just want to make sure we’re looking in the right place, can I see the line of code where you set your ‘character’ variable?
I don’t think there is anything wrong with the loop but try a debug by changing it from character to another part then putting parts in that part with different names to check if your loop works.
(my studio died lol)

Assuming, you aren’t using character correctly, here’s a fix.

for _,v in pairs(game.Workspace:GetDescendants()) do
	if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("BasePart") then
		if v.Name ~= "Baseplate" and v.CanCollide == true then
			character = Player.Character or Player.CharacterAdded:Wait()
			if not v:IsDescendantOf(character) then
				print(v)
				table.insert(table_1,v)	
			end
		
		end
	end
end
1 Like

doesnt work, i tried it. But it doesn’t work.

How are you checking whether the objects in the table are a descendent of character? (After the script runs)

by printing v. As shown in the code.

local function IsCharDesc(Object)
    if game:GetService("Players"):FindFirstChild(Object:GetFullName():match('Workspace.[^%.]+')) then
        return true;
    end
    return false;
end

for _,Object in pairs(game.Workspace:GetDescendants()) do
    if Object:IsA("Part") or Object:IsA("MeshPart") or Object:IsA("BasePart") then
        if Object.Name ~= "Baseplate" and Object.CanCollide == true then
            if not IsCharDesc(Object) then
                print(Object)
                table.insert(table_1,Object)    
            end
        end
    end
end

This should work to your liking!

slight modifications

for _,v in pairs(workspace:GetDescendants()) do -- replaced "game.Workspace" with just "workspace" because it's easier (although it doesn't matter which one you have)
	if v:IsA("BasePart") then -- "BasePart" includes "Parts" "MeshParts" "Unions" etc
		if not v.Name == "Baseplate" and v.CanCollide then -- check if the name isn't "Baseplate" and check if collisions is enabled
			if not v:IsDescendantOf(character) then
				print(v)
				table.insert(table_1, v)	
			end
		end
	end
end

Actually workspace is a leftover from the old API days, you should be using game:GetService('Workspace') over workspace to be consistent with other services.

This is actually (not v.Name) == "Baseplate" in Lua, not not (v.Name == "Basplate") like in Python, so this will always be false as not x evalulates to a boolean and boolean == string is false.
Is there any reason for modifing it from x ~= y to not (x == y)?

local Workspace = game:GetService('Workspace')
for _, v in ipairs(Workspace:GetDescendants()) do
    if v:IsA("BasePart") and v.Name ~= "Baseplate" and v.CanCollide and not v:IsDescendantOf(character) then
        print(v)
        table.insert(table_1, v)
   end
end
2 Likes

In my experience, using not x == y in Luau was more reliable than x ~= y as sometimes it would pass as “true” when it wasn’t

I’d disagree with this, in fact i’ve switched from game.Workspace to workspace. First off there’s a subtly difference between them:

  • workspace is a global variable
  • game.Workspace is a property of the DataModel
  • game:GetService("Workspace") is a method of fetching the service.

They all have the same result but game:GetService(“Workspace”) is pointless as is game:GetService(“Players”) as those two services will always exist on server start - GetService should be used for services which may not exist all the time, game.Workspace or workspace are fine, I prefer workspace as it’s simpler.

2 Likes

How, how is that even possible?

1 Like

I don’t know, some if statement checks I did had a different outcome with x ~= y compared to not x == y. I just use x ~= y when it comes to type checking

i edited your code and did this
it works 100% just tested myself

local table_1 = {}

wait(3)

for _,v in pairs(game.Workspace:GetDescendants()) do
	if v:IsA("BasePart") and not v:IsDescendantOf(game.Players.LocalPlayer.Character) then
		if v.Name ~= "Baseplate" and v.CanCollide == true then
			print(v)
			table.insert(table_1,v)	
		end
	end
end

We need to know what did you set character to. Please, open the output so you can debug, and tell us any errors you’ve got. Try this:

local Characters = {}

for _, Player in ipairs(game:GetService('Players'):GetPlayers()) do
    local Character = Player.Character or Player.CharacterAdded:Wait()
    table.insert(Characters, Character)
end

local table_1 = {}

for _, Descendant in ipairs(game.Workspace:GetDescendants()) do
	if Descendant:IsA("BasePart") then
		if Descendant.Name ~= "Baseplate" and Descendant.CanCollide == true then
			for _, Character in pairs(Characters) do
                if not Descendant:IsDescendantOf(Character) then
                    table.insert(table_1, Descendant) 
                end
            end
		end
	end
end