Solution for using ProximityPrompt with rectangular objects

I made this script to solve my own issue in script support:

Based on this answer, I made a variation of the @sleitnick script (https://twitter.com/sleitnick/status/1402644650452791299) to fire ProximityPrompt with rectangular objects.

This is because the original solution demands your character to ENTER a part to fire the Touched event. Since the goal is that this event is triggered by “Proximity”, this demands a greater distance between the object and the character.

This solution will basically create a new invisible part that will surround the current part, with its original size added by 5 studs (2.5 studs margin for each face). This will ensure that the ProximityPrompt event is fired 2.5 studs away from the face of the object, without the character not having to touch the original object. Of course, you can change this margin in the code.

This code will scan all objects that have a ProximityPrompt as a child and then make the necessary adaptations.

Here the sample project: ProxPromptRectangular.rbxl (28.0 KB)

Here the code:

local ProximityPromptService = game:GetService("ProximityPromptService")
local Players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local MARGIN = 5 -- will create a 2.5 margin (5 / 2) for each part's face. Change for the value you want

for _, ProxPrompt in ipairs(workspace:GetDescendants()) do -- for each ProximityPrompt found
	if ProxPrompt.Name == "ProximityPrompt" then
		local Part = ProxPrompt.Parent -- part with ProximityPrompt as child
		local PartTouch = Instance.new("Part")
		PartTouch.Parent = Part
		PartTouch.Name = Part.Name .. "Touch"
		PartTouch.CFrame = Part.CFrame
		PartTouch.Size = Vector3.new(Part.Size.X + MARGIN, Part.Size.Y + MARGIN, Part.Size.Z + MARGIN)
		PartTouch.Transparency = 0.5 -- 0.5 only for demonstration purpose. Set to 1
		PartTouch.CanCollide = false
		PartTouch.Anchored = true
		Part.CanTouch = false
		ProxPrompt.Parent = PartTouch
		ProxPrompt.RequiresLineOfSight = false
		ProxPrompt.MaxActivationDistance = 0

		local function IsRoot(CharacterPart)
			return Player.Character and CharacterPart == Player.Character.PrimaryPart -- if the part is the character's "HumanoidRootPart"
		end

		PartTouch.Touched:Connect(function(CharacterPart)
			if IsRoot(CharacterPart) then
				ProxPrompt.MaxActivationDistance = 10000
			end
		end)

		PartTouch.TouchEnded:Connect(function(CharacterPart)
			if IsRoot(CharacterPart) then
				ProxPrompt.MaxActivationDistance = 0
			end
		end)
	end
end

-- ProximityPromptService
ProximityPromptService.PromptTriggered:Connect(function(ProxPrompt, player)
	print("PromptTriggered")
	local OriginalPart = ProxPrompt.Parent.Parent.Name
end)

ProximityPromptService.PromptShown:Connect(function(ProxPrompt, InputType)
	print("PromptShown")
end)

ProximityPromptService.PromptHidden:Connect(function(ProxPrompt)
	print("PromptHidden")
end)
4 Likes