Returning all values from a for loop

Hey,

The title kind of explains the entire topic, but I am having troubles with this in my code. I have looked throughout the devforum, but no luck.

Code:

local function CharacterBaseParts()
		for _, BaseParts in ipairs(Character:GetChildren()) do 
			if BaseParts:IsA("BasePart") then 
				return BaseParts
			end
		end
	end
	
	local BaseParts = CharacterBaseParts()

It only returns one base part, but I need it to return all of them. How would I go about doing this?

The return will cancel the loop. Maybe putting all the baseparts into a table then after the loop returning the table with the baseparts?

1 Like
local function CharacterBaseParts()
    local Table = {}
		for _, BaseParts in ipairs(Character:GetChildren()) do 
			if BaseParts:IsA("BasePart") then 
				table.insert(Table, BaseParts)		
			end
		end
    return Table
end
4 Likes