Code Issues - Show/Hide ProximityPrompt

Hello everyone! I would like to get some help. What I need to do is make the proximity prompt visible only for “Team 1” and not for the rest of the teams. What am I doing wrong? Currently, it keeps showing for everyone.

image

localscript in element2:

-- Get the ProximityPrompt
local proximityPrompt = script.Parent:FindFirstChildOfClass("ProximityPrompt")

-- Define the team name that should see the ProximityPrompt
local teamNameToShowPrompt = "Team 1"  -- Replace with your team's name

-- Connect to the PromptShown event of the ProximityPrompt
proximityPrompt.PromptShown:Connect(function(player)
    print("PromptShown event triggered for " .. player.Name)

    -- Check if the player belongs to the specific team
    local character = player.Character
    local humanoid = character and character:FindFirstChildOfClass("Humanoid")
    local team = humanoid and humanoid.Team

    if team then
        print(player.Name .. "'s team: " .. team.Name)

        if team.Name == teamNameToShowPrompt then
            -- If the player is in the correct team, allow the ProximityPrompt to be shown
            print(player.Name .. " from team " .. teamNameToShowPrompt .. " sees the ProximityPrompt")
            return true
        else
            -- If the player is not in the correct team, prevent the ProximityPrompt from being shown
            print(player.Name .. " does NOT belong to team " .. teamNameToShowPrompt .. " and doesn't see the ProximityPrompt")
            return false
        end
    else
        print(player.Name .. " doesn't have an assigned team and doesn't see the ProximityPrompt")
        return false
    end
end)
2 Likes

You should use RemoteEvents and FireClient to make this possible.

Hide/Show the prompt on the Client and run the event by server.

E.x

local RemoteEvent = -- Location

RemoteEvent.OnClientEvent:Connect(function(Prompt, State)
   Prompt.Enabled = State
end)
local RemoteEvent = -- Location

RemoteEvent:FireClient(Player, Prompt, true)

I tried to do this, but it’s not working. What could be wrong? :frowning:

server script:

-- ServerScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- Create a RemoteEvent for communication between server and client
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt")

local elementProximityPrompt = script.Parent.ElementoProxPrompt

-- Connect to the PromptShown event of the ProximityPrompt
elementProximityPrompt.PromptShown:Connect(function(player)
	-- Check if the player belongs to the specific team
	local character = player.Character
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")
	local team = humanoid and humanoid.Team

	if team and team.Name == "Team 2" then
		-- If the player is in the correct team, trigger the event to the client with true value
		remoteEvent:FireClient(player, elementProximityPrompt, true)
		print(player.Name .. " from Team1 sees the ProximityPrompt")
	else
		-- If the player is not in the correct team, trigger the event to the client with false value
		remoteEvent:FireClient(player, elementProximityPrompt, false)
		print(player.Name .. " does NOT belong to Team1 and doesn't see the ProximityPrompt")
	end
end)

localscript:

-- LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- Wait for the RemoteEvent to exist in ReplicatedStorage
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt")

-- Connect to the remote event to handle the visibility of the ProximityPrompt on the client
remoteEvent.OnClientEvent:Connect(function(prompt, state)
	-- Set the visibility of the ProximityPrompt based on the received value
	prompt.Enabled = state
	print("ProximityPrompt visibility set to " .. tostring(state))
end)

Why are you getting the Team from the Humanoid instead of the Player?

I just made that change, and the proximity prompt is still showing. I want to hide it for Team 1 to test.

image

server:

-- ServerScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- Create a RemoteEvent for communication between server and client
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt")

local elementProximityPrompt = script.Parent.ElementoProxPrompt

-- Connect to the PromptShown event of the ProximityPrompt
elementProximityPrompt.PromptShown:Connect(function(player)
	-- Check if the player belongs to the specific team
	local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
	local team = humanoid and humanoid.Team

	if team and team.Name == "Team 2" then
		-- If the player is in the correct team, trigger the event to the client with true value
		remoteEvent:FireClient(player, elementProximityPrompt, true)
		print(player.Name .. " from Team 2 sees the ProximityPrompt. Event fired with visibility set to true.")
	else
		-- If the player is not in the correct team, trigger the event to the client with false value
		remoteEvent:FireClient(player, elementProximityPrompt, false)
		print(player.Name .. " does NOT belong to Team 2 and doesn't see the ProximityPrompt. Event fired with visibility set to false.")
	end
end)

localscript:

-- LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- Wait for the RemoteEvent to exist in ReplicatedStorage
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt")

-- Connect to the remote event to handle the visibility of the ProximityPrompt on the client
remoteEvent.OnClientEvent:Connect(function(prompt, state)
	-- Set the visibility of the ProximityPrompt based on the received value
	prompt.Enabled = state
	print("ProximityPrompt visibility set to " .. tostring(state))
end)

– ServerScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- Create a RemoteEvent for communication between server and client
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt")

local elementProximityPrompt = script.Parent.ElementoProxPrompt

-- Connect to the PromptShown event of the ProximityPrompt
elementProximityPrompt.PromptShown:Connect(function(player)
	-- Check if the player belongs to the specific team
	local team = player.Team

	if team and team == game.Teams["Team 2"] then
		-- If the player is in the correct team, trigger the event to the client with true value
		remoteEvent:FireClient(player, elementProximityPrompt, true)
		print(player.Name .. " from Team 2 sees the ProximityPrompt. Event fired with visibility set to true.")
	elseif team ~= game.Teams["Team 2"] then
		-- If the player is not in the correct team, trigger the event to the client with false value
		remoteEvent:FireClient(player, elementProximityPrompt, false)
		print(player.Name .. " does NOT belong to Team 2 and doesn't see the ProximityPrompt. Event fired with visibility set to false.")
	end
end)

– Local Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- Wait for the RemoteEvent to exist in ReplicatedStorage
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt")

-- Connect to the remote event to handle the visibility of the ProximityPrompt on the client
remoteEvent.OnClientEvent:Connect(function(prompt, state)
	-- Set the visibility of the ProximityPrompt based on the received value
	prompt.Enabled = state
	print("ProximityPrompt visibility set to " .. tostring(state))
end)

Or you might aswell just make a loop inside of a Client-sided script:

while task.wait() do
   for _, Players in pairs(game.Players:GetPlayers()) do
      if Players.Team == game.Teams["Team 2"] then
          for _, Prompts in pairs(workspace:GetDescendants()) do
              if Prompts.Name == "ElementoProxPrompt" then
game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt"):FireClient(Players, Prompts, true)
              end
          end
      elseif Players.Team ~= game.Teams["Team 2"] then
          for _, Prompts in pairs(workspace:GetDescendants()) do
              if Prompts.Name == "ElementoProxPrompt" then
game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt"):FireClient(Players, Prompts, false)
              end
          end
      end
   end
end

I tried your code, and it didn’t work. I also adjusted my code to this new format, and it still didn’t work. :confused:

image

updated version

script:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt")

local elementProximityPrompt = script.Parent.ElementoProxPrompt

local function UpdatePlayersPromptVisibility()
	for _, player in pairs(Players:GetPlayers()) do
		if player.Team == Teams["Team 2"] then
			remoteEvent:FireClient(player, elementProximityPrompt, true)
		else
			remoteEvent:FireClient(player, elementProximityPrompt, false)
		end
	end
end

UpdatePlayersPromptVisibility()

localscript:

local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt")

remoteEvent.OnClientEvent:Connect(function(prompt, status)
	prompt.Enabled = status
	print("ProximityPrompt visibility set to " .. tostring(status))
end)

The issue is that you re running the Client-Sided script from the Workspace.
Move the LocalScript to the StarterGui or StarterPlayerScripts.

note

Client-Sided scripts cannot run in the Workspace

I just did it, and it still doesn’t work. :frowning:

A script i made and it works for me.

on the client

local Player = game.Players.LocalPlayer;
wait();

local function CheckTeam()
	for _, Prompts in pairs(workspace:GetDescendants()) do
		if Prompts:IsA("ProximityPrompt") and Prompts.Name == Player.Team.Name then
			Prompts.Enabled = true
		elseif Prompts:IsA("ProximityPrompt") and Prompts.Name ~= Player.Team.Name then
			Prompts.Enabled = false	
		end
	end
end

CheckTeam();

You have to name the prompt to team’s name tho

Oh, look, I just got an error from the client.

  11:05:53.515  Invalid or nil ProximityPrompt received from server. Received: nil  -  Cliente - LocalScript:15

script:

-- Server Script

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("showProxPrompt")

local elementProximityPrompt = script.Parent:WaitForChild("ElementoProxPrompt")

-- Print the value of elementProximityPrompt before sending it to clients
print("Initial elementProximityPrompt value:", elementProximityPrompt)

local function UpdatePlayersPromptVisibility()
	for _, player in pairs(Players:GetPlayers()) do
		-- Check if the player is in Team 2
		if player.Team == Teams["Team 2"] then
			-- Fire the remote event to show the ProximityPrompt for players in Team 2
			remoteEvent:FireClient(player, elementProximityPrompt, true)
		else
			-- Fire the remote event to hide the ProximityPrompt for players not in Team 2
			remoteEvent:FireClient(player, elementProximityPrompt, false)
		end
	end
end

-- Call the function when a player is added to the game
Players.PlayerAdded:Connect(function(player)
	UpdatePlayersPromptVisibility()
end)

localscript:

-- LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("showProxPrompt")

remoteEvent.OnClientEvent:Connect(function(prompt, status)
	-- Check if the received prompt is a valid ProximityPrompt
	if typeof(prompt) == "Instance" and prompt:IsA("ProximityPrompt") then
		-- Set the visibility of the ProximityPrompt based on the received status
		prompt.Enabled = status
		print("ProximityPrompt visibility set to " .. tostring(status))
	else
		-- Warn if an invalid or nil ProximityPrompt is received
		warn("Invalid or nil ProximityPrompt received from server. Received:", prompt)
	end
end)
if typeof(prompt) == "userdata" and prompt:IsA("ProximityPrompt") then

still same issue :frowning:

  11:12:28.979  Invalid or nil ProximityPrompt received from server. Received: nil  -  Cliente - LocalScript:15

I pretty much have tested your script and its working perfect for me.

Oh, really? But do you have it organized like this? And how do you get the proximity prompt in the code?

I only have put the LocalScript inside of StarterGui and the Script inside of the ProximityPrompt and edited only like 2 lines of it.

p.s:
It’s also working with the type being userdata or Instance

Oh, I understand. Do you think you could send me the full code to check it out, please? :pray:

I will leave you the solution for the constant help, thank you very much

It worked for a minute and isn’t working anymore so i prefer naming the Prompts to the Team’s name and Enable/Disable them by the Client-side

or just check if the Player clicking the prompt is in that Team