Need help with a touch part problem

i am hoping this will be my last asking for help topic in this category but anyways, I’ve been trying to get a GUI to appear when a player touches a part, but not just one part, multiples. basically, i am using :GetChildren() to create a table of all the touch parts in a folder so that way, when a player touches either part, the GUI will appear

here is the script that i am using

local TouchPart = game.Workspace.Elevator:FindFirstChild("Touch"):GetChildren()
 
local Player = game.Players.LocalPlayer

function touched(hit)
	local plrname = hit.Parent.Name
	local Plr = game.Players:FindFirstChild(plrname)
	if Plr then
		Plr.PlayerGui.ElTpGui.MainFrame.Visible = true
	end
end

function notTouch(hit2)
	local plrname = hit2.Parent.Name
	local Plr = game.Players:FindFirstChild(plrname)
	if Plr then
		Plr.PlayerGui.ElTpGui.MainFrame.Visible = false
	end
end

TouchPart.Touched:Connect(touched)
TouchPart.TouchedEnded:Connect(notTouch)

if anyone could explain the issue to me or do whatever yall do, i’d really appreciate it

TouchPart represents a table here, so I see the issue.

Although, I have a few more notes:

Instead, do -

function touched(hit)
    if not hit.Parent:FindFirstChild("Humanoid") and not game:GetService("Players"):FindFirstChild(hit.Parent) then return end
	local plr = hit.Parent
	local Plr = game.Players:GetPlayerFromCharacter(plr)
	if Plr then
		Plr.PlayerGui.ElTpGui.MainFrame.Visible = true
	end
end

I am getting an error when I start the game

 Players.ScriptedDevPerson.PlayerScripts.ElevatorGui:14: attempt to index nil with 'Connect'

That is because TouchPart is a table. Try to assign it to a single part.
Something like -

local TouchPart = game.Workspace:WaitForChild("Elevator"):FindFirstChild("Touch")

Touch is a folder, it is not a part just to clarify.

I also renamed all of the parts in the folder to “TouchPart” to make it less confusing.

I know, that’s why it doesnt work.

If you want it to work for each part there,
use a for loop - something like this-

for _,part in pairs(TouchPart) do
   part.Touched:Connect(touched)
end

Youch, I am now getting an error

Players.ScriptedDevPerson.PlayerScripts.ElevatorGui:14: invalid argument #1 to 'pairs' (table expected, got Instance)

Could this be because the LocalScript is in the StarterPlayerScripts and not ServerScriptService? That has nothing to do with that, does it?

And I apologize

local TouchPart = game.Workspace.Elevator:FindFirstChild("Touch"):GetChildren()

Make sure you have this like that [as a table]

thanks man, it all works now. :slight_smile: