Proximity Prompts being handled from one script

So, I am testing out proximity prompts and i was wondering if there is a way I can put all my coins with proximity prompts in them, into a folder and if a player picks up one of the coins with the proximity prompt then it finds their name and does the function.

I know how to use proximity prompts with just a script in side each coin… but I wanted to see if I could do it with one whole script.

Why not just clone/copy 1 coin with the script inside of it?

local folder = workspace:WaitForChild("Folder")

for _, prompt in ipairs(folder:GetDescendants()) do
	if prompt:IsA("ProximityPrompt") then
		prompt.Triggered:Connect(function(player)
			print(player.Name.." picked up a coin!")
			--do stuff
		end)
	end
end

This is possible though. Will need to be a server script.

2 Likes

Was thinking about that, and I am going to be cloning parts into the group once one disappears and ther e is going to be a cap on how many can spawn in at once. But, I thought if I had like 40 parts and 1 script in side each part wouldnt that be excessive?

It is best to use as few scripts as possible, yes, the above will allow you to have 40+ prompts (or however many you decide) all tied to the same script.

Also wasn’t far off then. I had the direction path of the folder
and I had the for v loop but just wasn’t sure about the last part thank you. legend.

Oh one more question how would I modify the part that was interacted?

local folder = workspace:WaitForChild("Folder")

for _, prompt in ipairs(folder:GetDescendants()) do
	if prompt:IsA("ProximityPrompt") then
		prompt.Triggered:Connect(function(player)
			local part = prompt.Parent
			print(player.Name.." picked up a coin!")
			--do stuff
		end)
	end
end

Providing the prompt is inside the part.

ah yes I forgot v = the parts child