Hello! I coded this system where it should set the velocity of a part to the nearest player. For some reason its not working and it just goes down. Can someone help?
local Players = game:GetService("Players")
local part = clone
local maxDistance = 100
wait(1)
local nearestPlayer, nearestDistance
for _, player in pairs(game.Teams.Playing:GetPlayers()) do
local character = player.Character
local distance = player:DistanceFromCharacter(part.Position)
if not character or
distance > maxDistance or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
end
print("The nearest player is ", nearestPlayer)
clone.CFrame = CFrame.lookAt(clone.Position, nearestPlayer.Character:GetPivot().Position)
clone.Velocity = nearestPlayer.Character.HumanoidRootPart.CFrame.LookVector*script.Parent.Speed.Value