I am currently working on a resource genereration system for when a user harvests a generated resource. A ProximityPrompt is used for the user to harvest it and when a new resource is generated, the ProximityPrompt is added the the Prompts table.
I understand that the issue is that the Prompts table used in the for loop is using a copy of the Prompts table, which is why it doesn’t update when a new ProximityPrompt is added to the table.
My question is how to solve that issue? How do I update the table used in the for loop with the most recent table? Thank you.
local Prompts = {}
for i,Prompt in pairs(Prompts) do
if Prompt:IsA("ProximityPrompt") then
Prompt.Triggered:Connect(function(Player)
....
end)
end
end
I think you need to elaborate more…
What happens in game and how do you generate new resources?
As many details as possible would be helpful 
Are you doing some object oriented programming with modules? If so, did you setmetatables?
Your script only activates once, and I assume that the resources will spawn over time.
This solution will require the ProximityPrompt to be inside an instance (preferably a BasePart of some sort) to collect, but you can edit it as much as you need. It also requires a folder for the resources to spawn in (which is the way that they get detected).
Here’s the possible solution:
local folder = workspace:WaitForChild("Resources")
folder.ChildAdded:Connect(function(child)
local prompt = child:FindFirstChildWhichIsA("ProximityPrompt")
if prompt then
prompt.Triggered:Connect(function(player)
--code
end)
end
end)
Sorry about not including enough details. The code is quite extensive. All the resources are generated when a server is created. Each resource gets a ProxPrompt added as child. For the initially generated resources, the ProxPrompt works because they are in the Prompts table. As resources are harvested, the prompt is removed from the table. When new resources are generated to replace the lost resources, new prompts are created for those resources, which are also added to the Prompts table. The problem is that the newly added prompts are not being read in the for loop because the table is a copy of the first iteration of the Prompts table.
No metatables. I am not familiar with them. Should I research how to use them?
Thank you for the reply. I didn’t include the full code for this system because it is quite extensive. The whole resource spawning and harvesting system works except for newly added resources because even though the ProxPrompts are added to the Prompts table, it doesn’t get read in the for loop because the for loop uses a copy of the table that can’t update.
If you’re instancing an object through a module just make sure you add Module.__index = Module and setmetatables(newModule, Module)
If you’re not doing that ignore this.
Also here’s an easier way to do it:
game:GetService("ProximityPromptService").PromptTriggered:Connect(function(pressedprompt)
if table.find(prompts, pressedprompt) then
-- function
end
end)
``