How would i find the farthest part from the player

i have a script that does choose a brick to walk to but not the farthest and once it starts chasing the player again it will go back to that same part when done and not the farthest

function hide()
local furthest, relative = nil, 0
for _, path in pairs(game.Workspace.ChoosablePaths:GetChildren()) do
local pos = path.Position
local d1, d2 = (Bot.PrimaryPart.Position - pos).magnitude, (Goal.Character.PrimaryPart.Position - pos).magnitude
local r = d1/d2 – relative distance
if not furthest or relative >= r then
furthest = path
relative = r
print(furthest.Position)
while game.ReplicatedStorage.Rand.Value == 2 do
Path:Run(furthest.Position)
end
end
end
return furthest
end

please help because im lost

If you just want to choose a brick within the ChoosablePaths directory for the bot to walk to, you can write a simple little function that does just that:

local function choose_furthest_brick()
	local bot_pos = Bot.PrimaryPart.Position;

	local furthest_data = {nil, 0.0};
	for _, part in next, workspace.ChoosablePaths:GetChildren() do
		local dist = (part.Position - bot_pos).Magnitude;
		if dist > furthest_data[2] then
			furthest_data[1] = part;
			furthest_data[2] = dist;
		end
	end
	return furthest_data[1];
end

When you implement this, you should then re-structure your hide function to include logic that just makes the bot move and other related behaviour. My function returns the brick which is the furthest away, so you will need to access its position in order for the bot to run to it.

Have fun :^)

1 Like