Hiya!
Im attempting to make a tool that makes proximity prompts enable on other players when the player holding the tool equips it, So long as it meets these requirements:
- They’re within a set distance.
- They’re the closest player to the player holding the tool.
Im running this through two scripts, A serverscriptservice script that creates the prompts:
--[[ Vairables ]]--
local players = game:GetService("Players")
--[[ Functions ]]--
function AddProxPrompt(character)
local ArrestPrompt = Instance.new("ProximityPrompt")
ArrestPrompt.ActionText = ""
ArrestPrompt.ObjectText = ""
ArrestPrompt.Name = "ArrestPrompt"
ArrestPrompt.MaxActivationDistance = 1000
ArrestPrompt.HoldDuration = 1
ArrestPrompt.Enabled = false
ArrestPrompt.Parent = character:WaitForChild("HumanoidRootPart")
end
--[[ Player Added ]]--
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
AddProxPrompt(character)
end)
end)
And a local script inside the tool:
--[[ Variables ]]--
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Tool = script.Parent
--[[ Functions ]]--
function findClosestPlayer()
local character = Player.Character
if not character then
return nil
end
local toolPosition = Tool.Handle.Position
local closestPlayer = nil
local closestDistance = 10 -- Change this value to your desired radius
for _, otherPlayer in pairs(Players:GetPlayers()) do
if otherPlayer ~= Player then
local otherCharacter = otherPlayer.Character
if otherCharacter and otherCharacter:FindFirstChild("HumanoidRootPart") then
local distance = (toolPosition - otherCharacter.HumanoidRootPart.Position).Magnitude
if distance < closestDistance then
closestPlayer = otherPlayer
closestDistance = distance
end
end
end
end
return closestPlayer
end
function ShowPrompt()
local ClosestPlayer = findClosestPlayer()
if ClosestPlayer and ClosestPlayer.Character:WaitForChild("HumanoidRootPart"):FindFirstChild("ArrestPrompt") then
local prompt = ClosestPlayer.Character:WaitForChild("HumanoidRootPart"):FindFirstChild("ArrestPrompt")
prompt.Enabled = true
end
end
--[[ Equipped ]]--
Tool.Equipped:Connect(function()
while true do
wait(0.5)
ShowPrompt()
end
end)
The Issue:
Everything seems to work, it’s properly detecting the other player yet whenever it tries to enable the prompt it doesn’t become visible for the user holding the tool. Any idea on how I could fix this?