Basically trying to get the bot to pathfind to the nearest player at all times, but
Also not sure why it just decided to start flying but yeah
Heres the code
local Players = game.Players
local DisableAllBots = false
local Debug = false
local PathFindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local function FindNearest(NBC)
local NearestPlayer
local NearestDistance
for _, Player in (Players:GetPlayers()) do
if Player.Character and Player.Character.Humanoid and Player.Character.Humanoid.Health ~= 0 then
local Mag = (Player.Character.HumanoidRootPart.Position - NBC.Position).Magnitude
print(0)
if Player.Character.IsInSafeZone.Value == false then
print(1)
if NearestDistance then
if Mag < NearestDistance then NearestDistance = Mag; NearestPlayer = Player.Character end
else
NearestDistance = Mag; NearestPlayer = Player.Character
end
print(2)
end
print(3)
end
end
return NearestPlayer, NearestDistance
end
local function pfm(NextBot)
local Path = PathFindingService:CreatePath({
['AgentRadius'] = 5,
['AgentHeight'] = 5,
['AgentCanJump'] = true,
Costs = {
Water = 20,
SmoothPlastic = 1,
ForceField = math.huge,
Untouchable = math.huge
}
})
local Waypoints
local NextWaypointIndex
local reachedConnection
local blockedConnection
local NearestPlr, NearestDistance = FindNearest(NextBot.Character)
if not NearestPlr or NearestDistance > NextBot.ChangeableValues.DetectionDistance.Value then
return "No Near Players"
else
local Yes, No = pcall(function()
Path = PathFindingService:FindPathAsync(NextBot.PrimaryPart.Position, NearestPlr.HumanoidRootPart.Position)
end)
if Yes and Path.Status == Enum.PathStatus.Success then
Waypoints = Path:GetWaypoints()
blockedConnection = Path.Blocked:Connect(function(BwI)
if BwI >= NextWaypointIndex then
blockedConnection:Disconnect()
pfm(NextBot)
end
end)
for i, v in ipairs(Waypoints) do
if v.Action == Enum.PathWaypointAction.Jump then
NextBot.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
local Part = Instance.new("Part", workspace.Extras)
Part.Shape = Enum.PartType.Ball
Part.Size = Vector3.new(1,1,1)
Part.Anchored = true
Part.Position = v.Position
Part.Material = Enum.Material.Neon
Part.Color = Color3.new(0,4,0)
Part.CanCollide = false
Part.Transparency = 0.5
NextBot.Humanoid:MoveTo(v.Position)
NextBot.Humanoid.MoveToFinished:Wait()
end
else
if Debug == true then warn("Failed To Compute A Valid Path, Did you make sure that the NextBot Isnt Trapped / Stuck?: " .. if No then No else "Nil") end
end
end
end
local function PathFindingMovement(NextBot)
local Run
while task.wait() do
if NextBot.Humanoid.Active.Value == true and DisableAllBots == false then
local D = pfm(NextBot)
if D and Debug == true then
print(D)
end
else
break
end
end
end
Unedited Code
Just the entire module, if for some reason you would need the rest of the code with it
-- The NextBot Handler Module --
local Players = game.Players
local DisableAllBots = false
local Debug = false
local PathFindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
-- Functions
local function FindNearest(NBC)
local NearestPlayer
local NearestDistance
for _, Player in (Players:GetPlayers()) do
if Player.Character and Player.Character.Humanoid and Player.Character.Humanoid.Health ~= 0 then
local Mag = (Player.Character.HumanoidRootPart.Position - NBC.Position).Magnitude
print(0)
if Player.Character.IsInSafeZone.Value == false then
print(1)
if NearestDistance then
if Mag < NearestDistance then NearestDistance = Mag; NearestPlayer = Player.Character end
else
NearestDistance = Mag; NearestPlayer = Player.Character
end
print(2)
end
print(3)
end
end
return NearestPlayer, NearestDistance
end
local function MoveToMovement(NextBot, ValFol)
local Stepped
local NearestPlayer, NearestDistance
Stepped = RunService.Heartbeat:Connect(function(DTime)
if NextBot.Humanoid.Active.Value == true then
NearestPlayer, NearestDistance = FindNearest(NextBot.Character)
if NearestPlayer and NearestDistance then
if NearestDistance <= ValFol.DetectionDistance.Value then
NextBot.Humanoid:MoveTo(NearestPlayer.HumanoidRootPart.Position)
end
end
else
Stepped:Disconnect()
end
end)
end
local function pfm(NextBot)
local Path = PathFindingService:CreatePath({
['AgentRadius'] = 5,
['AgentHeight'] = 5,
['AgentCanJump'] = true,
Costs = {
Water = 20,
SmoothPlastic = 1,
ForceField = math.huge,
Untouchable = math.huge
}
})
local Waypoints
local NextWaypointIndex
local reachedConnection
local blockedConnection
local NearestPlr, NearestDistance = FindNearest(NextBot.Character)
if not NearestPlr or NearestDistance > NextBot.ChangeableValues.DetectionDistance.Value then
return "No Near Players"
else
local Yes, No = pcall(function()
Path = PathFindingService:FindPathAsync(NextBot.PrimaryPart.Position, NearestPlr.HumanoidRootPart.Position)
end)
if Yes and Path.Status == Enum.PathStatus.Success then
Waypoints = Path:GetWaypoints()
blockedConnection = Path.Blocked:Connect(function(BwI)
if BwI >= NextWaypointIndex then
blockedConnection:Disconnect()
pfm(NextBot)
end
end)
for i, v in ipairs(Waypoints) do
if v.Action == Enum.PathWaypointAction.Jump then
NextBot.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
local Part = Instance.new("Part", workspace.Extras)
Part.Shape = Enum.PartType.Ball
Part.Size = Vector3.new(1,1,1)
Part.Anchored = true
Part.Position = v.Position
Part.Material = Enum.Material.Neon
Part.Color = Color3.new(0,4,0)
Part.CanCollide = false
Part.Transparency = 0.5
NextBot.Humanoid:MoveTo(v.Position)
NextBot.Humanoid.MoveToFinished:Wait()
end
else
if Debug == true then warn("Failed To Compute A Valid Path, Did you make sure that the NextBot Isnt Trapped / Stuck?: " .. if No then No else "Nil") end
end
end
end
local function PathFindingMovement(NextBot)
local Run
while task.wait() do
if NextBot.Humanoid.Active.Value == true and DisableAllBots == false then
local D = pfm(NextBot)
if D and Debug == true then
print(D)
end
else
break
end
end
end
-- Module
local Nbot = {}
Nbot.Setup = function(NextBot)
NextBot.PrimaryPart:SetNetworkOwner(nil)
local ValFol = NextBot.ChangeableValues
local NextBotSpeed = ValFol.Speed.Value
local NextBotJumpPower = ValFol.JumpPower.Value
local NextBotHealth = ValFol.Health.Value
local CanBeKilled = ValFol.CanBeKilled.Value
local Pic = ValFol.Image.Value
local NBotHumanoid = NextBot.Humanoid
NBotHumanoid.WalkSpeed = NextBotSpeed
NBotHumanoid.JumpPower = NextBotJumpPower
if CanBeKilled == true then
NBotHumanoid.MaxHealth = NextBotHealth; NBotHumanoid.Health = NextBotHealth
else
local ff = Instance.new("ForceField", NextBot)
ff.Visible = false
NBotHumanoid.MaxHealth = math.huge; NBotHumanoid.Health = math.huge
end
NextBot.Character.Front.ImageLabel.Image = "rbxassetid://" .. Pic
NextBot.Character.Back.ImageLabel.Image = "rbxassetid://" .. Pic
if not NextBot.Humanoid:GetAttribute("Active") then
NextBot.Humanoid:SetAttribute("Active", false)
end
return "Setup Complete"
end
Nbot.Start = function(NextBot)
local ValFol = NextBot.ChangeableValues
NextBot.Humanoid.Active.Value = true
if ValFol.SmartAi.Value == true then
PathFindingMovement(NextBot)
print("Smart Movement Activated")
else
MoveToMovement(NextBot, ValFol)
print("Basic Movement Activated")
end
end
Nbot.Stop = function(NextBot)
NextBot.Humanoid.Active.Value = false
end
Nbot.DisableAllBots = function(A)
if A then
print("This function doesnt require anything to be sent, disables all bots")
end
if DisableAllBots == true then
print("Bots are already disabled")
return "Failed, Bots Are Disabled Already"
end
DisableAllBots = true
return "Bots Disabled"
end
Nbot.EnableAllBots = function(A)
if A then
print("This function doesnt require anything to be sent, enables all bots")
end
if DisableAllBots == false then
print("Bots are already enabled")
return "Failed, Bots Are Enabled Already"
end
DisableAllBots = false
return "Bots Enabled"
end
return Nbot