Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local players = game:GetService("Players")
- local pathfindingService = game:GetService("PathfindingService")
- local userInputService = game:GetService("UserInputService")
- local camera = workspace.CurrentCamera
- local player = players.LocalPlayer
- local character = player.Character or player.CharacterAdded:Wait()
- local humanoid = character:WaitForChild("Humanoid")
- local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
- local Range = 10 -- Range for detecting parts in the viewport
- local followDistance = 5 -- Distance to maintain from the nearest player
- local isRunning = false -- Toggle state
- local path = nil -- Path object for pathfinding
- -- Function to get the nearest player
- local function getNearestPlayer()
- local nearestPlayer = nil
- local nearestDistance = math.huge
- for _, otherPlayer in ipairs(players:GetPlayers()) do
- if otherPlayer ~= player and otherPlayer.Character then
- local otherHRP = otherPlayer.Character:FindFirstChild("HumanoidRootPart")
- local otherHumanoid = otherPlayer.Character:FindFirstChild("Humanoid")
- if otherHRP and otherHumanoid and otherHumanoid.Health > 0 then
- local distance = (humanoidRootPart.Position - otherHRP.Position).Magnitude
- if distance < nearestDistance then
- nearestDistance = distance
- nearestPlayer = otherPlayer
- end
- end
- end
- end
- return nearestPlayer
- end
- -- Function to use pathfinding to follow the nearest player
- local function followNearestPlayer()
- local targetPlayer = getNearestPlayer()
- if targetPlayer and targetPlayer.Character then
- local targetHumanoid = targetPlayer.Character:FindFirstChild("Humanoid")
- local targetHRP = targetPlayer.Character:FindFirstChild("HumanoidRootPart")
- if targetHumanoid and targetHumanoid.Health > 0 and targetHRP then
- local destination = targetHRP.Position - (targetHRP.CFrame.LookVector * followDistance)
- -- Create a path to the target
- path = pathfindingService:CreatePath({
- AgentRadius = humanoid.HipHeight,
- AgentHeight = humanoid.HipHeight * 2,
- AgentCanJump = true,
- AgentJumpHeight = humanoid.JumpPower,
- AgentMaxSlope = humanoid.MaxSlopeAngle,
- })
- path:ComputeAsync(humanoidRootPart.Position, destination)
- -- Check if the path is completed
- if path.Status == Enum.PathStatus.Success then
- for _, waypoint in pairs(path:GetWaypoints()) do
- if not isRunning then break end -- Stop if toggle is disabled
- if waypoint.Action == Enum.PathWaypointAction.Jump then
- humanoid.Jump = true
- end
- humanoid:MoveTo(waypoint.Position)
- humanoid.MoveToFinished:Wait()
- end
- else
- warn("Pathfinding failed.")
- end
- end
- end
- end
- -- Function to get parts in the viewport within a certain range
- local function getPartsInViewport(maxDistance)
- local partsInViewport = {}
- for _, part in ipairs(workspace:GetDescendants()) do
- if part:IsA("BasePart") then
- local distance = player:DistanceFromCharacter(part.Position)
- if distance <= maxDistance then
- local _, isVisible = camera:WorldToViewportPoint(part.Position)
- if isVisible then
- table.insert(partsInViewport, part)
- end
- end
- end
- end
- return partsInViewport
- end
- -- Function to activate tools on visible parts
- local function activateToolOnParts()
- local tool = player.Character and player.Character:FindFirstChildOfClass("Tool")
- local parts = getPartsInViewport(Range)
- if tool and tool:FindFirstChild("Handle") then
- for _, part in ipairs(parts) do
- if part and part.Parent and part.Parent ~= player.Character and part.Parent:FindFirstChildWhichIsA("Humanoid") and part.Parent:FindFirstChildWhichIsA("Humanoid").Health > 0 then
- tool:Activate()
- firetouchinterest(tool.Handle, part, 0)
- firetouchinterest(tool.Handle, part, 1)
- end
- end
- end
- end
- -- Function to handle the combined execution logic
- local function combinedExecution()
- while isRunning do
- followNearestPlayer()
- activateToolOnParts()
- wait(0.1) -- Small delay to avoid overloading
- end
- end
- -- Function to toggle the execution state
- local function toggleExecution()
- isRunning = not isRunning -- Toggle the state
- if isRunning then
- print("Execution started.")
- combinedExecution()
- else
- print("Execution stopped.")
- if path then
- path:Destroy()
- end
- end
- end
- -- Keybind for toggling execution
- userInputService.InputBegan:Connect(function(input, gameProcessed)
- if not gameProcessed and input.KeyCode == Enum.KeyCode.C then
- toggleExecution()
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement