Is there a better way to do this (handling multiple proximity prompts)

so i have these parts which should show/hide proximity prompts but when i use a for loop for handling the hide/show events for each prompt it doesnt work. is there a better/ more efficient way of doing what i did?

function module.PromptVisibility(p)
	local plrgui = p.PlayerGui
	local claim_UI = plrgui:FindFirstChild("claim_UI")
	local tree_1 = tree_folder and tree_folder:WaitForChild("tree_1")
	local tree_2 = tree_folder and tree_folder:WaitForChild("tree_2")
	local tree_3 = tree_folder and tree_folder:WaitForChild("tree_3")
	local tree_4 = tree_folder and tree_folder:WaitForChild("tree_4")
	local tree_5 = tree_folder and tree_folder:WaitForChild("tree_5")

	tree_1.proximity_prompt.PromptShown:Connect(function()
		claim_UI.Adornee = tree_1
		claim_UI.Enabled = true
	end)
	tree_1.proximity_prompt.PromptHidden:Connect(function()
		claim_UI.Adornee = nil
		claim_UI.Enabled = false
	end)
	tree_2.proximity_prompt.PromptShown:Connect(function()
		claim_UI.Adornee = tree_2
		claim_UI.Enabled = true
	end)
	tree_2.proximity_prompt.PromptHidden:Connect(function()
		claim_UI.Adornee = nil
		claim_UI.Enabled = false
	end)
	tree_3.proximity_prompt.PromptShown:Connect(function()
		claim_UI.Adornee = tree_3
		claim_UI.Enabled = true
	end)
	tree_3.proximity_prompt.PromptHidden:Connect(function()
		claim_UI.Adornee = nil
		claim_UI.Enabled = false
	end)
	tree_4.proximity_prompt.PromptShown:Connect(function()
		claim_UI.Adornee = tree_4
		claim_UI.Enabled = true
	end)
	tree_4.proximity_prompt.PromptHidden:Connect(function()
		claim_UI.Adornee = nil
		claim_UI.Enabled = false
	end)
	tree_5.proximity_prompt.PromptShown:Connect(function()
		claim_UI.Adornee = tree_5
		claim_UI.Enabled = true
	end)
	tree_5.proximity_prompt.PromptHidden:Connect(function()
		claim_UI.Adornee = nil
		claim_UI.Enabled = false
	end)
end

1 Like

Well, you could use a loop to go through each tree and perform the connection that way, but an even better solution in my opinion is to just use ProximityPromptService.PromptShown.

Here’s a link to that part of the documentation: ProximityPromptService | Documentation - Roblox Creator Hub

Just see the events section and use PromptShown, then you can handle all prompts without having to manually find each one in the workspace.

3 Likes

hey mate,
thanks for your help.

i tried a for loop yesterday but sometimes it doesnt show/hide it’s a bit weird :triumph:

function module.PromptVisibility()
	local function claimTree(p)
		print("Tree was claimed by: " .. p.Name .. "!")
	end

	for _, tree in pairs(tree_folder:GetChildren()) do
		if tree:IsA("BasePart") then
			local claim_UI = tree:FindFirstChild("claim_UI")
			if claim_UI and claim_UI:IsA("BillboardGui") then
				for _, instance in pairs(tree:GetChildren()) do
					if instance:IsA("ProximityPrompt") then
						instance.PromptShown:Connect(function()
							claim_UI.Enabled = true
						end)

						instance.PromptHidden:Connect(function()
							claim_UI.Enabled = false
						end)
					end
				end
			end
		end
	end
end

the issue would probably lie within the tree models. and right now your checking if tree is a basepart. im assuming its a model since its named tree, but anyways. if changing that to model doesnt work send a screen shot of the inside of the tree model

1 Like

hey green, thanks for your help mate.

basically theyre like circle parts but theyre gonna spawn trees hence why i named it that way but its a folder.

i tried other stuff to get the for loop to work but unfortunately it doesnt. i managed to clean up the script a lil bit tho so it works like this:

function module.PromptVisibility(p)
	local plrgui = p.PlayerGui
	local claim_UI = plrgui:FindFirstChild("claim_UI")
	local tree_1 = tree_folder and tree_folder:WaitForChild("tree_1")
	local tree_2 = tree_folder and tree_folder:WaitForChild("tree_2")
	local tree_3 = tree_folder and tree_folder:WaitForChild("tree_3")
	local tree_4 = tree_folder and tree_folder:WaitForChild("tree_4")
	local tree_5 = tree_folder and tree_folder:WaitForChild("tree_5")

	local function handlePrompt(tree)
		local prompt:ProximityPrompt = tree:FindFirstChild("proximity_prompt")

		prompt.PromptShown:Connect(function()
			claim_UI.Adornee = tree
			claim_UI.Enabled = true
		end)

		prompt.PromptHidden:Connect(function()
			claim_UI.Adornee = nil
			claim_UI.Enabled = false
		end)
	end
	
	handlePrompt(tree_1)
	handlePrompt(tree_2)
	handlePrompt(tree_3)
	handlePrompt(tree_4)
	handlePrompt(tree_5)
end

Okay so I don’t quite understand what the issue is. Im assuming its the ui not showing/hiding right?
Can you add prints in these to events saying “shown” and “hidden” respectively, and tell me what it prints?
image

1 Like

thats mb bro, yup ur spot on, it doesn’t show / hide. in a for loop i cant get it to work but with the script above it works perfectly fine.

this was my original script but it doesnt really work when i use a for loop:

function module.PromptVisibility()
	local function claimTree(p)
		print("Tree was claimed by: " .. p.Name .. "!")
	end

	for _, tree in pairs(tree_folder:GetChildren()) do
		if tree:IsA("BasePart") then
			local claim_UI = tree:FindFirstChild("claim_UI")
			if claim_UI and claim_UI:IsA("BillboardGui") then
				for _, instance in pairs(tree:GetChildren()) do
					if instance:IsA("ProximityPrompt") then
						instance.PromptShown:Connect(function()
							claim_UI.Adornee = tree
							claim_UI.Enabled = true
							print("showing")
						end)

						instance.PromptHidden:Connect(function()
							claim_UI.Adornee = nil
							claim_UI.Enabled = false
							print("hiding")
						end)
					end
				end
			end
		end
	end
end

and the behaviour:

Like I said, you can use a loop but I can’t recommend it.

Please see the attached link I provided in my initial response. ProximityPromptService lets you provide handling for all proximity prompts rather than index them directly; you’ll find it probably solves your issue if not please let me know with a code sample attached.

1 Like

ohhh shoot i didnt know there was a service for proximityprompt. i didnt know about its uses until now. thanks a million for your help mate!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.