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)
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)
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
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)
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)
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…
Are you sure this is doing what you actually want it to do? I’m fairly certain it isn’t?
Issues I can see:
You’re creating a new Heartbeat connection every time the PlayerJoinedEvent is called without checking to see if one already exists for that player
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
You don’t clean up the proximity prompt(s) if the player is no longer near the other player
You don’t check to see whether the proximity prompt for that player exists which means you’re creating multiple proximity prompts across frames
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)