Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PathfindingService = game:GetService("PathfindingService")
- local Players = game:GetService("Players")
- local RunService = game:GetService("RunService")
- local path = PathfindingService:CreatePath({
- AgentHeight = 12;
- AgentRadius = 6;
- AgentCanJump = false;
- Costs = {
- Water = 100;
- DangerZone = math.huge
- }
- })
- local Character = script.Parent
- local humanoid = Character:WaitForChild("Humanoid")
- local waypoints
- local nextWaypointIndex
- local reachedConnection
- local blockedConnection
- local function findTarget()
- local maxDistance = 500
- local nearestTarget
- for index, player in pairs(Players:GetPlayers()) do
- if player.Character then
- local target = player.Character
- local distance = (Character.HumanoidRootPart.Position
- - target.HumanoidRootPart.Position).Magnitude
- if distance < maxDistance and target.Humanoid.Health > 0 then
- nearestTarget = target
- maxDistance = distance
- end
- if distance < 5 then
- nearestTarget.Humanoid:TakeDamage(25)
- end
- end
- end
- return nearestTarget
- end
- local function FollowPath(destination)
- local success, errorMessage = pcall(function()
- path:ComputeAsync(Character.PrimaryPart.Position, destination)
- end)
- if success and path.Status == Enum.PathStatus.Success then
- waypoints = path:GetWaypoints()
- blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
- if blockedWaypointIndex >= nextWaypointIndex then
- blockedConnection:Disconnect()
- FollowPath(destination)
- end
- end)
- if not reachedConnection then
- reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
- if reached and nextWaypointIndex < #waypoints then
- nextWaypointIndex += 1
- humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
- else
- reachedConnection:Disconnect()
- blockedConnection:Disconnect()
- end
- end)
- end
- nextWaypointIndex = 2
- humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
- else
- warn("Path not computed", errorMessage)
- end
- end
- while wait() do
- local target = findTarget()
- if target then
- print(target.Name)
- FollowPath(target.HumanoidRootPart.Position)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement