Pathfinding ignore summoner

HI devforum, Im currently making a tool which when used, summons a bomb which pathfinds to the nearest player. However, immidietley upon summon it pathfinds to the summoner. How would i make it so the pathfinding ignores the summoner

script

local rep = game.ReplicatedStorage
local EWeapons = rep.ReplicatedCommon.WeaponEvents
local Weapons = rep.Weapons
game.Players.PlayerAdded:Connect(function(player)
local function summon()

local Bomb = Weapons.TripSwitchBomb:Clone()
	local CF = player.Character.HumanoidRootPart.CFrame * CFrame.new(0, 0.5, -4)
	Bomb.Parent = workspace
	Bomb.HumanoidRootPart.CFrame = CF

	
	--Services--
	local RunService = game:GetService('RunService')
	local PathfindingService = game:GetService('PathfindingService')

	--Variable--
	local hum = Bomb.Humanoid
	local humrp = Bomb.HumanoidRootPart


	--Pathfinding
	Bomb.PrimaryPart:SetNetworkOwner(nil)
	local destination = workspace.wp.Position

	local function findTarget()
		local players = game.Players:GetPlayers()
		local MaxDistance = 200
		local nearestTarget

		for i,v in pairs(players) do
			if (v.Character) then
				local target = v.Character
				local distance = (humrp.Position - target.HumanoidRootPart.Position).Magnitude

				if (distance < MaxDistance) then
					nearestTarget = target
					MaxDistance = distance
				end
			end
		end

		return nearestTarget
	end

	local function agro(target)
		local distance = (humrp.Position - target.HumanoidRootPart.Position).Magnitude

		if (distance > 8) then
			hum:MoveTo(target.HumanoidRootPart.Position)
		else

			target.Humanoid.Health = 0
			local Explosion = Instance.new("Explosion")
			Explosion.Parent = workspace
			Explosion.ExplosionType = Enum.ExplosionType.NoCraters
			Explosion.BlastRadius = 3
			Explosion.DestroyJointRadiusPercent = 0
			Explosion.Position = Bomb.HumanoidRootPart.Position	
		Bomb:Destroy()
		end
	end

	local function createPath(destination)
		local path = PathfindingService:CreatePath()
		path:ComputeAsync(humrp.Position, destination)

		return path
	end

	local function walkTo(destination)
		local path = createPath(destination)

		local waypoints = path:GetWaypoints()

		for i,v in pairs(waypoints) do
			local target = findTarget()

			if (target) then
				agro(target)
				break
			end

			hum:MoveTo(v.Position)

			hum.MoveToFinished:Wait()
		end
	end

	while task.wait(0.5) do
		walkTo(destination)
	end
	
end

game.ReplicatedStorage.ReplicatedCommon.WeaponEvents.summonBomb.OnServerEvent:Connect(function(player)
summon()
end)
end)

local function findTarget()
    ...
end

The issue is in this function. It is checking for the closest player to the bomb, not taking into account the user who placed the bomb. Simply ignore user who placed the bomb while making the nearest player check.

ive tried doing this but the repeat until keeps timing out because for somereason it cant not have it the player who is closest

local function findTarget()
local players = game.Players:GetPlayers()
local MaxDistance = 200
local nearestTarget

		for i,v in pairs(players) do
			repeat until v.Name ~= player.Name
				if (v.Character) then
				
				local target = v.Character
				local distance = (humrp.Position - target.HumanoidRootPart.Position).Magnitude
		
				if (distance < MaxDistance) then
					nearestTarget = target
					MaxDistance = distance
				end
			end
		end

No repeat until necessary.

local function findTarget()
		local players = game.Players:GetPlayers()
		local MaxDistance = 200
		local nearestTarget

		for i,v in pairs(players) do
            -- You would add the check here. Define "user" as the person who has placed the bomb
			if (v.Character and v.Character ~= user) then
				local target = v.Character
				local distance = (humrp.Position - target.HumanoidRootPart.Position).Magnitude

				if (distance < MaxDistance) then
					nearestTarget = target
					MaxDistance = distance
				end
			end
		end

		return nearestTarget
	end

now the pathfinding works but it still targets the summoner

	local function findTarget()
		local players = game.Players:GetPlayers()
		local MaxDistance = 200
		local nearestTarget

		for i,v in pairs(players) do
			local user = player
			if (v.Character and v.Character ~= user) then
				local target = v.Character
				local distance = (humrp.Position - target.HumanoidRootPart.Position).Magnitude
		
				if (distance < MaxDistance) then
					nearestTarget = target
					MaxDistance = distance
				end
			end
		end
local players = game.Players:GetPlayers()
table.remove(players, user)
1 Like

would that be just after ```
for i,v in pairs(players) do

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