I am trying to get a suspect closest to an officer, (player closest to player on team) so I can enable that player’s gui closest to the officer so the local player can see it, basically like in meepcity if you want to pick up a baby you walk up to them and press E
The issue is I am making sure it is the issue because when I print it it shows up as the officer trying to get the suspect closest to them ex: im a player and i come near another player and press E, the script thinks its me im pressing E on not the player im closest to
I’ve tried a lot of solutions like switching the players, this haven’t worked, please help me
local Players = game:GetService("Players")
local OfficerPlayerYes = game.Players.LocalPlayer
local InpuService = game:GetService("UserInputService")
local ReplicatedEventsFolder = game.ReplicatedStorage.Static
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 65
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
InpuService.InputBegan:Connect(function(keypresse, gamprocces)
local SuspectPlayer = findNearestTorso(OfficerPlayerYes.Character.HumanoidRootPart.Position)
if keypresse.KeyCode == Enum.KeyCode.C and not gamprocces then
if SuspectPlayer ~= nil then
if OfficerPlayerYes.Team == game.Teams.Wardens then
if (SuspectPlayer.Position - OfficerPlayerYes.Character.HumanoidRootPart.Position).Magnitude <=8 then
print(SuspectPlayer.Parent)
ReplicatedEventsFolder.Events.Cuff:FireServer(SuspectPlayer)
end
end
end
end
end)
--[[ InpuService.InputBegan:Connect(function(KeyPressed, gamproce)
local SuspectPlayer = findNearestTorso(OfficerPlayerYes.Character.HumanoidRootPart.Position)
if KeyPressed.KeyCode == Enum.KeyCode.G and not gamproce then
if SuspectPlayer ~= nil then
if OfficerPlayerYes.Team == game.Teams.Wardens then
if (SuspectPlayer.Position - OfficerPlayerYes.Character.HumanoidRootPart.Position).Magnitude <=6 then
ReplicatedEventsFolder.Grab:FireServer(SuspectPlayer)
end
end
end
end
end) ]]--
while true do
wait(2)
local SuspectPlayer = findNearestTorso(OfficerPlayerYes.Character.HumanoidRootPart.Position)
if SuspectPlayer ~= nil then
if OfficerPlayerYes.Team == game.Teams.Wardens then
if (SuspectPlayer.Position - OfficerPlayerYes.Character.HumanoidRootPart.Position).Magnitude <=8 then
print(SuspectPlayer.Parent)
SuspectPlayer:WaitForChild("OfficerControllerPrompt").Enabled = true
end
end
end
end