Why doesn't this script give the player that is in front of me?

So, I’m trying to make it so that if a player is in front of another player, the ProximityPrompt should be given to the player in front, but instead, it gives it to me

It’s a server script

code:

PlayerJoinedEvent.OnServerEvent:Connect(function(player)
	RunService.Heartbeat:Connect(function(delta)
		if player.Character  then
			
			
			local PlayerHrp  = player.Character:FindFirstChild("HumanoidRootPart")

			for _, v in game.Players:GetPlayers() do
				if v.Character then
					local TargetHRP = v.Character:FindFirstChild("HumanoidRootPart")
					
					local RayParam = RaycastParams.new()
					RayParam.FilterDescendantsInstances = {player.Character}
					RayParam.FilterType = Enum.RaycastFilterType.Exclude
					
					local Direction = PlayerHrp.CFrame.LookVector * 7
					
					local Raycast = workspace:Raycast(PlayerHrp.Position, Direction, RayParam)
					
					if (Raycast and Raycast.Instance) and Raycast.Instance.Parent.Name == v.Name then
						if DistanceCheck(TargetHRP, PlayerHrp, player, v) then
							Create_ProximityPrompt(v)

						
						end

					end
					

				end
				
			end
		end
	end)
	
	task.wait(.2)
end)

please soemone help

the issue might be with the RaycastParams and the Raycast usage.

PlayerJoinedEvent.OnServerEvent:Connect(function(player)
    RunService.Heartbeat:Connect(function(delta)
        if player.Character then
            local PlayerHrp = player.Character:FindFirstChild("HumanoidRootPart")

            for _, v in game.Players:GetPlayers() do
                if v ~= player and v.Character then
                    local TargetHRP = v.Character:FindFirstChild("HumanoidRootPart")

                    local RayParam = RaycastParams.new()
                    RayParam.FilterDescendantsInstances = {v.Character}
                    RayParam.FilterType = Enum.RaycastFilterType.Blacklist

                    local Direction = PlayerHrp.CFrame.LookVector * 7

                    local Raycast = workspace:Raycast(PlayerHrp.Position, Direction, RayParam)

                    if Raycast and Raycast.Instance and Raycast.Instance.Parent == v.Character then
                        if DistanceCheck(TargetHRP, PlayerHrp, player, v) then
                            Create_ProximityPrompt(v)
                        end
                    end
                end
            end
        end
    end)

    task.wait(.2)
end)

Nope, it’s still the same thing. I also tried it without raycasting, but it still gives it to me instead of the player in front.

Why are you excluding the descendants of the player’s character, if I’m not wrong, you wanted to include them?

PlayerJoinedEvent.OnServerEvent:Connect(function(player)
	RunService.Heartbeat:Connect(function(delta)
		if player.Character  then
			
			
			local PlayerHrp  = player.Character:FindFirstChild("HumanoidRootPart")

			for _, v in game.Players:GetPlayers() do
				if v.Character then
					local TargetHRP = v.Character:FindFirstChild("HumanoidRootPart")
					
					local RayParam = RaycastParams.new()
					RayParam.FilterDescendantsInstances = {v.Character}
					RayParam.FilterType = Enum.RaycastFilterType.Include -- these are the parts we are looking for
					
					local Direction = PlayerHrp.CFrame.LookVector * 7
					
					local Raycast = workspace:Raycast(PlayerHrp.Position, Direction, RayParam)
					
					if (Raycast and Raycast.Instance) and Raycast.Instance.Parent.Name == v.Name then
						if DistanceCheck(TargetHRP, PlayerHrp, player, v) then
							Create_ProximityPrompt(v)

						
						end

					end

yea still the same results


I want Player 1 to receive the proximity prompt.

try this

                    if Raycast and Raycast.Instance:IsDescendantOf(v.Character) then
                        if DistanceCheck(TargetHRP, PlayerHrp, player, v) then
                            Create_ProximityPrompt(v)
                        end

still the same results :weary:

full script:

function Create_ProximityPrompt(Target : Player)
	if Target then
		local TargetHRP = Target.Character:FindFirstChild("HumanoidRootPart")
		
		local ProximityPrompt = Instance.new("ProximityPrompt", TargetHRP)
		ProximityPrompt.Name = "PairPrompt"
		ProximityPrompt.ActionText = ""
		ProximityPrompt.MaxActivationDistance = 5
		
		if ProximityPrompt then
		end
		
	end
end

function DistanceCheck(TargetHRP, PlayerHrp, player : Player, v : Player)
	if player ~= v and (TargetHRP.Position - PlayerHrp.Position).Magnitude <= 5 then        
		return true
	end
	
	
end

PlayerJoinedEvent.OnServerEvent:Connect(function(player)
	RunService.Heartbeat:Connect(function(delta)
		if player.Character  then


			local PlayerHrp  = player.Character:FindFirstChild("HumanoidRootPart")

			for _, v in game.Players:GetPlayers() do
				if v.Character then
					local TargetHRP = v.Character:FindFirstChild("HumanoidRootPart")

					local RayParam = RaycastParams.new()
					RayParam.FilterDescendantsInstances = {v.Character}
					RayParam.FilterType = Enum.RaycastFilterType.Include 

					local Direction = PlayerHrp.CFrame.LookVector * 7

					local Raycast = workspace:Raycast(PlayerHrp.Position, Direction, RayParam)

					if Raycast and Raycast.Instance:IsDescendantOf(v.Character) then
						if DistanceCheck(TargetHRP, PlayerHrp, player, v) then
						Create_ProximityPrompt(v)
							end
						end
				end
			end
		end
	end)

	task.wait(.2)
end)
function Create_ProximityPrompt(Target)
    if Target and Target.Character then
        local TargetHRP = Target.Character:FindFirstChild("HumanoidRootPart")
        if TargetHRP then
            local ProximityPrompt = Instance.new("ProximityPrompt", TargetHRP)
            ProximityPrompt.Name = "PairPrompt"
            ProximityPrompt.ActionText = ""
            ProximityPrompt.MaxActivationDistance = 5
            
            -- ProximityPrompt
            ProximityPrompt.Triggered:Connect(function(player)
                print("ProximityPrompt triggered")
            end)
        end
    end
end

function DistanceCheck(TargetHRP, PlayerHRP)
    if TargetHRP and PlayerHRP then
        return (TargetHRP.Position - PlayerHRP.Position).Magnitude <= 5
    end
    return false
end

PlayerJoinedEvent.OnServerEvent:Connect(function(player)
    RunService.Heartbeat:Connect(function(delta)
        if player.Character then
            local PlayerHRP = player.Character:FindFirstChild("HumanoidRootPart")
            if PlayerHRP then
                for _, v in ipairs(game.Players:GetPlayers()) do
                    if v ~= player and v.Character then
                        local TargetHRP = v.Character:FindFirstChild("HumanoidRootPart")
                        if TargetHRP then
                            local RayParam = RaycastParams.new()
                            RayParam.FilterDescendantsInstances = {player.Character}
                            RayParam.FilterType = Enum.RaycastFilterType.Exclude

                            local Direction = PlayerHRP.CFrame.LookVector * 7
                            local Raycast = workspace:Raycast(PlayerHRP.Position, Direction, RayParam)

                            if Raycast and Raycast.Instance:IsDescendantOf(v.Character) and DistanceCheck(TargetHRP, PlayerHRP) then
                                Create_ProximityPrompt(v)
                            end
                        end
                    end
                end
            end
        end
    end)

    task.wait(0.2)
end)

It’s still the same. I still notice that I’m getting proximity prompts instead of the other player who’s in front. Why is it doing this? :frowning_face:

PlayerJoinedEvent.OnServerEvent:Connect(function(player)
    RunService.Heartbeat:Connect(function(delta)
        if player.Character then
            local PlayerHRP = player.Character:FindFirstChild("HumanoidRootPart")
            if PlayerHRP then
                for _, v in ipairs(game.Players:GetPlayers()) do
                    if v ~= player and v.Character then
                        local TargetHRP = v.Character:FindFirstChild("HumanoidRootPart")
                        if TargetHRP then
                            local RayParam = RaycastParams.new()
                            RayParam.FilterDescendantsInstances = {player.Character}
                            RayParam.FilterType = Enum.RaycastFilterType.Exclude

                            local Direction = PlayerHRP.CFrame.LookVector * 7
                            local Raycast = workspace:Raycast(PlayerHRP.Position, Direction, RayParam)

                            if Raycast and Raycast.Instance == TargetHRP and DistanceCheck(TargetHRP, PlayerHRP) then
                                Create_ProximityPrompt(v)
                            end
                        end
                    end
                end
            end
        end
    end)

    task.wait(0.2)
end)

its still the same could it be a bug? Why? :frowning_with_open_mouth: :frowning_with_open_mouth:

it’s probably some kind of error in the code. I can’t really see what’s wrong

1 Like

Try this. If it doesn’t work, then idk know what to tell

function Create_ProximityPrompt(Target, Player)
    if Player and Player.Character then
        local PlayerHRP = Player.Character:FindFirstChild("HumanoidRootPart")
        if PlayerHRP then
            local ProximityPrompt = Instance.new("ProximityPrompt", PlayerHRP)
            ProximityPrompt.Name = "PairPrompt"
            ProximityPrompt.ActionText = ""
            ProximityPrompt.MaxActivationDistance = 5
            
            -- ProximityPrompt
            ProximityPrompt.Triggered:Connect(function(player)
                print("ProximityPrompt triggered")
            end)
        end
    end
end

function DistanceCheck(TargetHRP, PlayerHRP)
    return (TargetHRP.Position - PlayerHRP.Position).Magnitude <= 5
end

PlayerJoinedEvent.OnServerEvent:Connect(function(player)
    RunService.Heartbeat:Connect(function(delta)
        if player.Character then
            local PlayerHRP = player.Character:FindFirstChild("HumanoidRootPart")
            if PlayerHRP then
                for _, v in ipairs(game.Players:GetPlayers()) do
                    if v ~= player and v.Character then
                        local TargetHRP = v.Character:FindFirstChild("HumanoidRootPart")
                        if TargetHRP then
                            local RayParam = RaycastParams.new()
                            RayParam.FilterDescendantsInstances = {player.Character}
                            RayParam.FilterType = Enum.RaycastFilterType.Exclude

                            local Direction = PlayerHRP.CFrame.LookVector * 7
                            local Raycast = workspace:Raycast(PlayerHRP.Position, Direction, RayParam)

                            if Raycast and Raycast.Instance == TargetHRP and DistanceCheck(TargetHRP, PlayerHRP) then
                                Create_ProximityPrompt(v, player)
                            end
                        end
                    end
                end
            end
        end
    end)

    task.wait(0.2)
end)

I found the bug apparently, it was also affecting the front player, although it wasn’t visible. I’m unsure why it was in both, as I could only see mine.

i will mark u as solution as urs worked too without visible…

but thanks to you!

1 Like

Are you sure this is doing what you actually want it to do? I’m fairly certain it isn’t?

Issues I can see:

  1. You’re creating a new Heartbeat connection every time the PlayerJoinedEvent is called without checking to see if one already exists for that player
  2. You’re casting the same ray multiple times for a single player because you’re doing it from within the second iterator when checking if the hit is a player
  3. You don’t clean up the proximity prompt(s) if the player is no longer near the other player
  4. You don’t check to see whether the proximity prompt for that player exists which means you’re creating multiple proximity prompts across frames
  5. You don’t clean up the event(s) which will cause a memory leak

Edit: Also, side note, the reason your player can’t see it is because the RequiresLineOfSight property for ProximityPrompts defaults to true


Code that implements changes to solve these issues:

Example Code
local RunService = game:GetService('RunService')
local PlayerService = game:GetService('Players')

local currentPrompts = { }
local currentPlayers = { }

local function tryGetCharacterFromPart(obj)
  if typeof(obj) ~= 'Instance' then
    return nil
  end

  local parent, humanoid = obj.Parent, nil
  while not humanoid and parent do
    humanoid = parent:FindFirstChildOfClass('Humanoid')
    parent = parent.Parent
  end

  if not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead or humanoid.Health <= 0 then
    return nil
  end

  local rootPart = humanoid.RootPart
  if not rootPart then
    return nil
  end

  return humanoid.Parent, humanoid, rootPart
end

local function tryGetCharacterInstance(player)
  local t = typeof(player)
  if t ~= 'Instance' or not player:IsA('Player') or not player:IsDescendantOf(PlayerService) then
    return false
  end

  local character = player.Character
  if not character or not character:IsDescendantOf(workspace) then
    return false
  end

  local humanoid = character:FindFirstChildOfClass('Humanoid')
  if not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead or humanoid.Health <= 0 then
    return false
  end

  local rootPart = humanoid.RootPart
  if not rootPart then
    return false
  end

  return true, character, rootPart
end

local function createProximityPrompt(targetPlayer, targetCharacter, targetRootPart)
  local ProximityPrompt = Instance.new("ProximityPrompt", targetRootPart)
  ProximityPrompt.Name = "PairPrompt"
  ProximityPrompt.ActionText = ""
  ProximityPrompt.RequiresLineOfSight = false
  ProximityPrompt.MaxActivationDistance = 5

  local triggerConnection
  triggerConnection = ProximityPrompt.Triggered:Connect(function(player)
    print("ProximityPrompt triggered")
  end)

  return ProximityPrompt, triggerConnection
end

local function distanceCheck(targetHRP, playerHRP, maxDistance)
  maxDistance = maxDistance or 5

  if targetHRP and playerHRP then
    return (targetHRP.Position - playerHRP.Position).Magnitude <= maxDistance
  end

  return false
end

PlayerJoinedEvent.OnServerEvent:Connect(function(player)
  local index = table.find(currentPlayers, player)
  if index then
    return
  end

  table.insert(currentPlayers, player)
end)

PlayerService.PlayerRemoving:Connect(function (player)
  local index = table.find(currentPlayers, player)
  if not index then
    return
  end

  local prompt = currentPrompts[player]
  if prompt then
    local promptObject = prompt.Object
    if promptObject then
      pcall(promptObject.Destroy, promptObject)
    end

    currentPrompts[player] = nil
  end
  table.remove(currentPlayers, index)
end)

RunService.Heartbeat:Connect(function (delta)
  local i = 1
  while i <= #currentPlayers do
    local player = currentPlayers[i]
    if not player then
      i += 1
      continue
    end

    local isAlive, character, rootPart = tryGetCharacterInstance(player)
    if not isAlive then
      local prompt = currentPrompts[player]
      if prompt then
        local promptObject = prompt.Object
        if promptObject then
          pcall(promptObject.Destroy, promptObject)
        end

        currentPrompts[player] = nil
      end

      i += 1
      continue
    end

    local rayParams = RaycastParams.new()
    rayParams.FilterType = Enum.RaycastFilterType.Exclude
    rayParams.FilterDescendantsInstances = { character }

    local prompt = currentPrompts[player]
    local origin = rootPart.CFrame
    local result = workspace:Raycast(origin.Position, origin.LookVector*7, rayParams)

    local foundTarget = false
    if result and result.Instance then
      local targetCharacter, targetHumanoid, targetRootPart = tryGetCharacterFromPart(result.Instance)
      if targetCharacter and distanceCheck(rootPart, targetRootPart) then
        local targetPlayer = PlayerService:GetPlayerFromCharacter(targetCharacter)
        if targetPlayer then
          if not prompt or prompt.Target ~= targetCharacter then
            if prompt and prompt.Object then
              pcall(prompt.Object.Destroy, prompt.Object)
            end

            local instance, triggerConnection = createProximityPrompt(targetPlayer, targetCharacter, targetRootPart)
            prompt = { Target = targetCharacter, Object = instance }
            currentPrompts[player] = prompt

            local deletedConnection
            deletedConnection = instance.Destroying:Connect(function ()
              if triggerConnection and triggerConnection.Connected then
                triggerConnection:Disconnect()
              end

              if deletedConnection and deletedConnection.Connected then
                deletedConnection:Disconnect()
              end

              if player and currentPlayers[player] == prompt then
                currentPlayers[player] = nil
              end
            end)
          end

          foundTarget = true
        end
      end
    end

    if not foundTarget and (prompt and prompt.Object) then
      local promptObject = prompt.Object
      pcall(promptObject.Destroy, promptObject)
      currentPrompts[player] = nil
    end

    i += 1
  end
end)

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