Help with player interaction script

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
local Game = game
local Players = Game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local function GetClosestPlayer()
	local Closest = {Player = nil, Distance = math.huge}
	for _, Player in ipairs(Players:GetPlayers()) do
		if Player == LocalPlayer then continue end
		local Character = Player.Character
		if not Character then continue end
		local Distance = LocalPlayer:DistanceFromCharacter(Character:GetPivot().Position)
		if Distance == 0 or Distance > Closest.Distance then continue end
		Closest.Player = Player
		Closest.Distance = Distance
	end
	return Closest
end

local Closest = GetClosestPlayer()
if Closest.Player then
	--Do code.
end
local Game = game
local Players = Game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local function GetClosestTeamPlayer()
	local Closest = {Player = nil, Distance = math.huge}
	local Team = LocalPlayer.Team
	if not Team then return Closest end
	for _, Player in ipairs(Team:GetPlayers()) do
		if Player == LocalPlayer then continue end
		local Character = Player.Character
		if not Character then continue end
		local Distance = LocalPlayer:DistanceFromCharacter(Character:GetPivot().Position)
		if Distance == 0 or Distance > Closest.Distance then continue end
		Closest.Player = Player
		Closest.Distance = Distance
	end
	return Closest
end

local Closest = GetClosestTeamPlayer()
if Closest.Player then
	--Do code.
end
1 Like

thank you this helped a lot I appreciate it

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