How do I make this return all values?

Hello developers!

Basically I’m trying to get all the parts shown here with this little script I made! It functions but only returns the first name, which is “1”.
image

Heres my code, but I need it to work with all of them, but it only returns the first value.

local UIS = game:GetService("UserInputService")
local key = Enum.KeyCode.E

--local UI = game.ServerStorage:WaitForChild("EToInteract")
local StudsAway = 15
local HRP = game.Players.LocalPlayer.Character:WaitForChild('HumanoidRootPart')
local playerInteracted = game.ReplicatedStorage:WaitForChild("PlayerInteracted")

local db = false

local function getAllInteractions()
	for i,v in pairs(workspace.E_Interactions:GetDescendants()) do
		if v:IsA('BasePart') and v.Name == 'E_Main' then
			return v
		end
	end
end
-- Main
UIS.InputBegan:Connect(function(input, isPlayerTyping)
	if input.KeyCode == key and isPlayerTyping == false and db == false then
		--if (getAllInteractions().Position - HRP.Position).magnitude < StudsAway then
			print(getAllInteractions().Parent.Name)
		--end
	end
end)

Thanks!

You can return an array containing all of the parts you want instead.

local function getAllInteractions()
	local t = {} -- {} creates a new, empty table. 
	for i,v in pairs(workspace.E_Interactions:GetDescendants()) do
		if v:IsA('BasePart') and v.Name == 'E_Main' then
			table.insert(t, v) -- this inserts v into the last position of t.
		end
	end
	return t
end

UIS.InputBegan:Connect(function(input, isPlayerTyping)
	if input.KeyCode == key and isPlayerTyping == false and db == false then
		local interactions = getAllInteractions()
		for _, interactionPart in ipairs(interactions) do -- ipairs iterates through the array.
			print(interactionPart.Parent.Name)
		end
	end
end)
1 Like

Never knew about “ipairs”. Thats pretty cool, thank you! :smiley: