RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"make a script that walks in the direction of the nearest enemy"
local pfs = game:GetService("PathfindingService") local character = game.Players.LocalPlayer.Character local humanoid = character:WaitForChild("Humanoid") local enemies = game.Workspace.Enemies:GetChildren() while true do local closestEnemy = nil local shortestDistance = math.huge for i, enemy in ipairs(enemies) do local distance = (character.HumanoidRootPart.Position - enemy.HumanoidRootPart.Position).Magnitude if distance < shortestDistance then closestEnemy = enemy shortestDistance = distance end end if closestEnemy then local path = pfs:CreatePath() path:ComputeAsync(character.HumanoidRootPart.Position, closestEnemy.HumanoidRootPart.Position) for _, waypoint in pairs(path:GetWaypoints()) do humanoid:MoveTo(waypoint.Position) humanoid.MoveToFinished:Wait() end end wait(1) end