Hello!
I am in the process of finalising a theatre, and am wanting the players to sit in seats by clicking a proxmity prompt that appears on each seat.
The issue is that if I were to do a basic prox prompt script in every single seat, I would be looking at over 200 scripts.
Is it possible to make around 200 proximity prompts function correctly with one script? How would I go about doing this?
To avoid having tons of scripts for every seat you could look into using CollectionService. You can add a tag to every seat and then loop through them like so:
local CollectionService = game:GetService("CollectionService")
for _, seat in CollectionService:GetTagged("Seat") do
local proximityPrompt = Instance.new("ProximityPrompt")
-- ...
proximityPrompt.Parent = seat
end
This is the method I would recommend you use. This is exactly what CollectionService was designed for, grouping a bunch of things together that you can reference with a single tagged name. This allows you to create them all together, access them all together, change their behaviour together, and ultimately destroy them all together. Simply by using the method showed by EntryRoot, i.e.:
CollectionService:GetTagged("Seat");
This will return a table that contains all Instances that have been tagged as “Seat”. Which you can then do anything with.
This is actually the most performant solution since the ‘PromptTriggered’ event of the ‘ProximityPromptService’ is silently fired whenever a prompt is triggered (regardless of whether or not a function has been hooked to it). No looping or tagging necessary, then as that reply suggested, you can simply look for the relevant seat (as an ancestor of the prompt) and act accordingly.
local Game = game
local PromptService = Game:GetService("ProximityPromptService")
local function OnPromptTriggered(Prompt, Player)
local Seat = Prompt:FindFirstAncestorOfClass("Seat")
if not Seat then return end
--Do code.
end
PromptService.PromptTriggered:Connect(OnPromptTriggered)
This is probably the best answer if all you want to do is trigger them. It doesn’t really show you a method of creating 200+ seats with proximity prompts in them, and it also doesn’t give you the benefit of doing something else other than triggering the prompt. Say, changing all of the seats colours, kicking all players out of the seats at the same time, or maybe flying them around the place with cool particles and smoke effects. Whereas CollectionService can do all of this.