Making a part go to the player but close to the player

So I am making a boss and it goes around the arena and it tweens to random parts. But I want it to tween to the random parts that the player is close to I don’t know how to achieve this!


The parts that are in this arena are the parts that the boss randomly chooses.

Also the boss has 2 fazes so when the boss gets in the arena it automatically starts with faze 1 then after some time it goes to faze 2.
Main Script:

local ts = game:GetService('TweenService')

local F1_Script = script.Parent:WaitForChild('F1')
local F2_Script = script.Parent:WaitForChild('F2')

local partsFolder = game.Workspace.BossParts
local parts = partsFolder:GetChildren()
local boss = script.Parent

local function appear()
	local function tween()
		local info = TweenInfo.new(2,Enum.EasingStyle.Elastic,Enum.EasingDirection.Out,0,false,0)
		local goal = {Transparency = 0, Position = Vector3.new(-25.902, 7.644, 156.334)}
		--
		local Tween = ts:Create(boss,info,goal)
		Tween:Play()
	end
	print("THE EYE BOSS HAS APPEARED!")
	tween()
end

wait(3)
appear()
wait(2.55)
F1_Script.Disabled = false
wait(8)
F1_Script.Disabled = true
wait()
print("Faze2!")
F2_Script.Disabled = false

Faze1 script:

local ts = game:GetService("TweenService")

local partsFolder = game.Workspace.BossParts
local parts = partsFolder:GetChildren()
local part = script.Parent

local wait1 = math.random(1.5,3)
local wait2 = math.random(1,2)

for _, aPart in ipairs(parts) do
	local new = ts:Create(part, TweenInfo.new(wait1,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out,0,false,0), {Position = aPart.Position})
	new:Play()
	new.Completed:Wait()
	task.wait(wait2)
end

Faze 2 script:
local ts = game:GetService(“TweenService”)

local partsFolder = game.Workspace.BossParts

local parts = partsFolder:GetChildren()

local part = script.Parent

local wait1 = math.random(2,4)

local wait2 = math.random(1,2)

for _, aPart in ipairs(parts) do

local new = ts:Create(part, TweenInfo.new(wait1,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out,0,false,0), {Position = aPart.Position})

new:Play()

new.Completed:Wait()

task.wait(wait2)

end

Have you considered adding some larger scale parts around the area where these parts that you have added are?
And having a touch event so you will know in which erea the player is and there for know wich part your boss should tween to.

To achieve this, first you’d have to find out which of those parts the player is closest to. Assuming all of those parts are under a folder, I’ll just guess it’s called BossPartsFolder, you could find it out this way:

local Players = game:GetService("Players")
local bossPartsFolder = workspace:WaitForChild("BossPartsFolder")

local allPartDistances = {}
local closestParts = {}

local function distanceBetween(p1, p2)
    return (p1.Position - p2.Position).Magnitude
end

local function getSmallestValue(array)
    local currentKey, currentValue = nil, 1e5
    for k, value in pairs(array) do
        if value < currentValue then
            currentKey = k
            currentValue = value
        end
    end
    return currentKey, currentValue
end

local function moveBoss()
    -- Find all of the closest parts per player
    for _, player in pairs(Players:GetPlayers()) do
        allPartDistances[player] = {}
        
        for _, part in pairs(bossPartsFolder:GetChildren()) do
            allPartDistances[player][part] = distanceBetween(player.Character.HumanoidRootPart, part)
        end
        
        closestParts[player] = getSmallestValue(allPartDistances[player])
    end
    
    -- Somewhere down here is where you'd decide which to tween the boss to
    -- Could be a random player, and just find their closest part like so:
    local randomPlayer = Players:GetPlayers()[math.random(#Players:GetPlayers())]
    local partToTweenBossTo = closestParts[randomPlayer]
    boss:MoveTo(partToTweenBossTo.Position)
    
    -- Or you could choose the player the boss is already closest to
    local playerDistancesToBoss = {}
    for _, player in pairs(Players:GetPlayers()) do
        playerDistancesToBoss[player] = distanceBetween(player.Character.HumanoidRootPart, boss.HumanoidRootPart)
    end
    local closestPlayer = getSmallestValue(playerDistancesToBoss)
    local partToTweenBossTo = closestParts[closestPlayer]
    boss:MoveTo(partToTweenBossTo)
end

Those were just some ways you could find out which part to teleport the boss to. I’m sure there are more creative solutions, and even some that might help balance the gameplay a little better. This is all just pseudo-code that I (assumed, but haven’t tested) think will work.

[Note]: This code, like stated above, is pseudo-coded and there is definitely some areas that can be touched up on if you wanted to use this in actual production. Namely, the fact that I’m assuming each player has a character and a HumanoidRootPart without actually checking. It would also make sense to check if the players are alive, if the random player chosen is inside of the closestParts dictionary before trying to index it. Things like that to make sure it wouldn’t bug out.

I will try your ideas on what you guys said!