Today, I wanted to make a ball detect a player nearby and run away.
There is a BodyForce inside of the ball to make it move.
The issue is that the formula doesn’t work so well.
I tried searching for some solutions but there isn’t any perfect results for this issue.
Formula:
Part.BodyForce.Force = (players[n].Character.HumanoidRootPart.Position)*-1
Function:
local function FindTarget()
local MaxDistance = 20
local BestTarget = nil
local players = game:GetService("Players"):GetPlayers()
for n = 1,#players do
if players[n].Character ~= nil and players[n].Character.HumanoidRootPart then
local dist = (players[n].Character.HumanoidRootPart.Position - Part.Position).magnitude
local Humanoid = players[n].Character:FindFirstChild("Humanoid")
if dist and dist < MaxDistance and Humanoid then
Part.BodyForce.Force = (players[n].Character.HumanoidRootPart.Position)*-1
else
Part.BodyForce.Force = Vector3.new(0,0,0)
end
break
end
end
end
Any help and feedback is appreciated!