Listening for an event from any member of a table?

I have some code I recycled from a class tutorial, and I’m trying to change the firing function from a touchevent to an activation of a proximityprompt. because the program doesn’t know how many activation switches/“doorknobs” the door actually needs, it uses a method to add virtual “doorknobs” to all attachments parented to the door object, and inserts a proximityprompt, parents it to the attachment, and adds the object to a table. However, I’m not sure how I’d detect the activation of these prompts from a single script, as having to manually add listening scripts every door isn’t an option. So I’m wondering, does anyone know any methods or have any ideas as to how I’d detect the event firing from any of the proximity prompts within the list?

func DoorTable.new(door object)
        local self = {}
	self.door = door
	self.debounce = false
	self.children = self.door:GetDescendants() 
	self.attachments = {}
	self.triggers = {}
	
	for i, x in pairs(self.children) do
		if x:IsA("Attachment") and (x.Name =="DoorTrigger") then
			table.insert(self.attachments,x)
		end
	end
	
	for i, x in pairs(self.attachments) do
		local z = Instance.new("ProximityPrompt")
		z.Parent = x
		z.ObjectText = "Door"
		z.HoldDuration = ".5"
		z.MaxActivationDistance = "5"
		table.insert(self.triggers, z)
	end
	

	self.touchConn = ***door.Touched***:Connect(function (...) --(what i want to change to detect firing of prxprompts)
		self:OnTouch(...)
	end)

Just put the Connect code inside of the pairs loop and that should be it to listen to any of the prompts

Though if you also want to, at some point disconnect that specific event, you’d need to also store it in a dictionary

for i, x in pairs(self.attachments) do
	local z = Instance.new("ProximityPrompt")
	z.Parent = x
	z.ObjectText = "Door"
	z.HoldDuration = ".5"
	z.MaxActivationDistance = "5"
	table.insert(self.triggers, z)
	
	self.connections[z] = z.Triggered:Connect(function(player)
		--Code
	end)
end
1 Like