My nextbots attack each other

Idk what’s wrong with my script. Can someone tell my why it’s happening and is there anyways to solve it?

“Chase” script:

local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage:WaitForChild("NextbotAI"))

local Goal = nil
local Bot = script.Parent
local Path = SimplePath.new(Bot)

function findnearestPlayer(Origin:Vector3,MaxDistance:number)
	local Players = game:GetService("Players")
	local NearestPlayer,NearestDistance

	for i,player in pairs(Players:GetPlayers()) do
		local character = player.Character
		local distance = player:DistanceFromCharacter(Origin)

		if not character or distance > MaxDistance or (NearestDistance and distance >= NearestDistance) then
			continue
		end
		

		NearestDistance = distance
		NearestPlayer = player
	end

	return NearestPlayer
end

while true do
	Goal = findnearestPlayer(Bot.PrimaryPart.Position,math.huge)
	if Goal then
		Path:Run(Goal.Character.PrimaryPart.Position)
		if (Goal.Character.PrimaryPart.Position - Bot.PrimaryPart.Position).Magnitude < 4 then
			local Humanoid = Goal.Character:FindFirstChildOfClass("bot")
			if Humanoid then
				Humanoid:TakeDamage(0) -- keep as 0
			end
		end
	end

	task.wait()
end

“Death” script (kill part):

local Copy = script.Parent:Clone()
local NPC = script.Parent
local Humanoid
for i,v in pairs(NPC:GetChildren()) do 
	if v:IsA('bot') then 
		Humanoid = v 
	end 
end

script.Parent.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = 0
		local character = hit.Parent
		local KnockBack = Instance.new("BodyVelocity")
		KnockBack.P = math.huge
		KnockBack.Parent = character:FindFirstChild("HumanoidRootPart")
		KnockBack.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		KnockBack.Velocity = character:FindFirstChild("HumanoidRootPart").CFrame.lookVector * -132
		game.Debris:AddItem(KnockBack, 0.5)
	else
		if hit.Name == "Detector" then
			Copy.Parent = NPC.Parent
			Copy:MakeJoints()
			NPC:Destroy()
		end
	end
end)
1 Like

There’s no logic to check if it’s a player, so you’ll have to add that. You can use game.Players:GetPlayerFromCharacter(hit.Parent) to figure out which player the character belongs to. If it doesn’t belong to a player then it returns nil. Try modifying the if statement to add this check.

Alright, I’ll look into it. Thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.