How to create a ball that tracks the nearest player and follows them around

i want to know how to create a ball that followes the nearest player. But personally I have no idea how to start.

I know it has to do with receiving the humanoid of a player ingame and going to the position they are, but how would this be scripted if the ball has no humanoid with their own walkingpeed to reach the player?

image
image

Anyhow thanks for reading and have a nice day!

1 Like

Hopefully this helps:

Made a file of the model so that you can download it without any hassle…
ModelPartThatFollows.rbxm (5.7 KB)

To implement the model into your game, you click on insert from file:

What it all looks like:

image

Code:

local runService = game:GetService("RunService")
local players = game:GetService("Players")

local humanoid = script.Parent
local root = humanoid.Parent.PrimaryPart

local targetDistance = math.huge -- follow distance.. (math.huge = an infinite number, you could make it 20 or something..)
local stopDistance = 10 -- stopping distance..

function FindNearestPlayer()
	local nearestPlayer = nil
	local distance = nil
	local direction = nil
	--
	for _, player in pairs(players:GetChildren()) do
		local character = player.Character
		if character then
			local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
			if not nearestPlayer then
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit
			elseif distanceVector.Magnitude < distance then
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit
			end	
		end
	end
	--
	return nearestPlayer, distance, direction
end

runService.Heartbeat:Connect(function()
	local nearestPlayer, distance, direction = FindNearestPlayer()
	--
	if nearestPlayer then
		if distance <= targetDistance and distance >= stopDistance then
			humanoid:Move(direction)
		else
			humanoid:Move(Vector3.new())
		end
	end
end)
1 Like

I appricate it alot but is there a way to do this without humanoid? You know with a ball chasing you around if you are the nearest player?

I’m sorry if my explanation was a bit confusing and I thank you alot to think with me along and even help me. It means alot!

Edit:
I found something related to this using BodyForce but it seems to be Deprecated. Even with it I would not know how to use it toward the goal I wish to reach.

You could use linear velocity and use CFrame.lookAt to constantly look at the player while the velocity pushes it forward (assuming part is unanchored)

If it’s anchored maybe just a loop of CFrame.new(part.Position, targetHRP.Position) * CFrame.new(0,0,1)

Typed on my phone*