Part flying directly at nearest player

Hello!

So basically i want to make a part that flies DIRECTLY at the nearest player it can find. So how would i do that? How do i make the part fly towards the player and switch targets if someone else gets closer

Thanks in advance

1 Like

You can try making a for i, v in pairs() loop that goes thru all the players, and then returns the nearest player value. you can use this as an example:

(i made a timestamp for you :grin:)

1 Like

you can make it fly by getting the nearest player’s cframe, and tweening it to that cframe. Keep in mind you need to have some type of while true do loop to keep it going!

1 Like

Wouldnt that not work if the player moves? Since it wouldnt update to the new player position and the tween would just continue

1 Like

like i told you, use some kind of while true do or game:GetService("RunService").RenderStepped:Connect(function() loop function so it updates

easy, here you go:

local players = Game.players
local distance = math.huge
local part = Script.Parent
local target = nil
for N, c in Ipairs(players:GetChildren()) do
    if (part.Position - c.Character.PrimaryPart.Position).Magnitude < distance then
         distance = (part.Position - c.Character.PrimaryPart.Position).Magnitude
         target = c
    end
end
part.CFrame = CFrame.LookAt(part.Position, target.Character.PrimaryPart.Position)
part.Velocity = part.LookVector * 10

put this in while true loop with wait(0.1) and it should work flawlessly (maybe some errors) but i think it should.

the part where you get the character is extremely inconsistent

sometimes it prints the actual character and the other times it prints “nil”

try doing Player:WaitForChild("Character")

My suggestion is to connect to game:GetService("RunService").Heartbeat and loop through each player in the server to determine who is closest to the position of the part. Afterwards, save the character of the closest player to a variable. You can then use an AlignPosition constraint to make the part fly towards the current closest character.

Something like this:

local part: BasePart = nil -- The path to the flying part
local alignPosition: AlignPosition = nil -- The path to the AlignPosition constraint you'll be using

local function getClosestCharacter()
    -- Declare closest character data
    local closestCharacter_Model = nil
    local closestCharacter_Distance = nil

    -- Loop through each player
    for i, v in game:GetService("Players"):GetPlayers() do
        -- Skip the player if they don't have a character
        local character = v.Character
        local hrp = character and character:FindFirstChild("HumanoidRootPart")
        if not hrp then continue end

        -- Compare the player's distance from the part to the current closest player
        local distance = (hrp.Position - part.Position).Magnitude
        if not distance and distance < closestCharacter_Distance then
            -- Save new data
            closestCharacter_Distance = distance
            closestCharacter_Model = character
        end
    end

    -- Update the align position constraint
    alignPosition.Position = closestCharacter:GetPivot().Position
end

game:GetService("RunService").Heartbeat:Connect(getClosestCharacter)

Disclaimer: This code is meant to simply get the idea across and will not work without some editing. In other words, you’ll have to make some changes to it for it to actually work.

PS - Like a previous post mentioned, while true loops can also be used instead of connecting to Heartbeat–just make sure you have a task.wait() inside of the loop so the code doesn’t exhaust itself and crash your Studio.

after combining your script with sam’s script it worked!!! thank you guys so much

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