Help disabling proximity prompt for a specific player?

I have a proximity prompt which gets instanced and parented to each player’s HRP, and I want it to the proximity prompt to be invisible to the player of which the proximity prompt is within, however I still want that one player to be able to see other player’s proximity prompts.

My current code doesn’t work with this functionality for some reason, disabling everyone’s proximity prompt.

Any help?

Server:

local function OnPlayerAdded(Player)
	local NewTemplate = PlayerRatingDataModule.Functions.CreateTemplate(Player)
	PlayerRatingDataModule.Data[Player.UserId] = NewTemplate
	local FetchedTemplate = PlayerRatingDataModule.Functions.FetchStoreTemplate(Player)
	if FetchedTemplate ~= nil then
		PlayerRatingDataModule.Data[Player.UserId] = FetchedTemplate
		NewTemplate = nil
	end
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	if HumanoidRootPart then
		local ProximityPrompt = Instance.new("ProximityPrompt")
		ProximityPrompt.Name = Player.Name.."'s Prompt"
		ProximityPrompt.Parent = HumanoidRootPart
		ProximityPrompt.ActionText = "Rate "..Player.Name.."'s Avatar"
		
		ProximityPrompt.ObjectText = Player.Name
		ProximityPrompt.HoldDuration = 3
		ProximityPrompt.Triggered:Connect(function(InvestigatingPlayer)
			game.ReplicatedStorage.PromptProfile:FireClient(InvestigatingPlayer, PlayerRatingDataModule.Data[Player.UserId])
		end)
	end
end

Local:

local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
HRP:WaitForChild(plr.Name.."'s Prompt").Enabled = false
for i, v in pairs(game.Players:GetPlayers()) do
	if v ~= plr then
		local Character = v.Character or v.CharacterAdded:Wait()
		local HRP = Character:WaitForChild("HumanoidRootPart")
		HRP:WaitForChild(v.Name.."'s Prompt").Enabled = true
	end
end

Help?

If the prompts are being created by the server, setting the enabled state of any prompt parented to the LocalPlayer's character to false in a LocalScript will disable it for that player without disabling it for everyone else.

If the enabled state of a trigger is ever updated by the server, that will replicate and override whatever you have set it to in the LocalScript. This can be circumvented by having the LocalScript parent the prompt to nil when unneeded or destroy it outright.

2 Likes

So…Like this?

local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
HRP:WaitForChild(plr.Name.."'s Prompt").Parent = nil
for i, v in pairs(game.Players:GetPlayers()) do
	if v ~= plr then
		local Character = v.Character or v.CharacterAdded:Wait()
		local HRP = Character:WaitForChild("HumanoidRootPart")
		HRP:WaitForChild(v.Name.."'s Prompt").Enabled = true
	end
end

Your ProximityPrompts that are created on the server should be initialized as enabled by default. As far as each client is concerned, it only needs to modify its own, and the server can be responsible for all of the other prompts. Try something like this (which would run in a LocalScript added to StarterPlayerScripts:

local heartbeat = game:GetService("RunService").Heartbeat
local player = game:GetService("Players").LocalPlayer
local remove_prompts_connection

local function remove_prompt(object)
	if object:IsA("ProximityPrompt") then
		--Objects received to an ancestry-related event require one frame of wait time minimum before their parent
		--value can be changed. I used Heartbeat here, but Stepped also works.
		heartbeat:Wait()
		object:Destroy()
	end
end

local function initialize_character(character)
	if remove_prompts_connection then
		remove_prompts_connection:Disconnect()
		remove_prompts_connection = nil
	end
	remove_prompts_connection = character.DescendantAdded:Connect(remove_prompt)
	for _,object in ipairs(character:GetDescendants()) do
		remove_prompt(object)
	end
end

player.CharacterAdded:Connect(initialize_character)
if player.Character then
	initialize_character(player.Character)
end

This block of code destroys any prompts parented to the LocalPlayer's character, and it should also handle the player respawning. It does its job in broad strokes, though, so if you ever need the LocalPlayer to have prompts within their character that they do need to interact with, the remove_prompt() function will need refining.

1 Like