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
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.
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
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
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
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.
I don’t know, someif 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