What is wrong with my AI script?

As you guys know,I wanna make a piggy game called Caty[Alpha]

The AI script:
local bot = script.Parent

local players = game.Players:GetPlayers()

local function killTarget()
	local attack = bot.Humanoid:LoadAnimation(script.attackAnim)
	local jumpscare = script.attackSound
	for index, player in pairs(players) do
		local character = player.Character
		
		local distance = (bot.HumanoidRootPart.Position - character.HumanoidRootPart.Position)
		if distance < 2 then
			if character then
				bot.HumanoidRootPart.Anchored = true
				character.HumanoidRootPart.Anchored = true
				jumpscare:Play()
				attack:Play()
				attack.Stopped:Wait(0.3)
				bot.HumanoidRootPart.Anchored = false
				character.HumanoidRootPart.Anchored = false
			end
		end
	end
end

local function pathSystem()
	for index, player in pairs(players) do
		local character = player.Character
		
		local pfs = game:GetService("PathfindingService")
		
		local path = pfs:CreatePath()
		
		path:ComputeAsync(bot.HumanoidRootPart.Position, character.HumanoidRootPart.Position)
		
		return path
	end
end

local function followTarget()
	for index, player in pairs(players) do
		local character = player.Character
		
		local path = pathSystem()
		
		for index, waypoint in pairs(path:GetWaypoints()) do
			local distance = (bot.HumanoidRootPart.Position - character.HumanoidRootPart.Position)
			if distance < 100000 then
				bot.Humanoid:MoveTo(character.HumanoidRootPart.Position)
				print("Target:" .. character.Name)
				killTarget()
			end
		end
	end
end

while wait(0.5) do
	followTarget()

end

Yor help is really appriciated!

Extra info:The script is in Script and not LocalScript or ModuleScript

Could you give some more info, what exactly is the problem?

1 Like

Like,the screenshot of the explorer?

No, if you have any errors in output or what is not working, did u try printing etc.?

1 Like

But after I press play,nothing happen

Did u try printing where it stops?

1 Like

nothing happen,the bot didnt moving

The potentional of print(“Something”) is to debug your code to get the “line” where it stops.

1 Like

Oh ok,let me try to fix it and i’ll tell you if it work

Is it possible that the humanoid is anchored?

1 Like

The HumanoidRootPart is not anchored also

Make sure the model is not anchored and copy the script below I created for you.

local bot = script.Parent
local attack = bot.Humanoid:LoadAnimation(script.attackAnim)
local jumpscare = script:WaitForChild("attackSound")

local Distance,PathBreakerDistance,AttackDistance = math.huge, 30, 5 --PathBreakerDistance is to prevent the NPC from having choppy movements when approaching it's target

local function killTarget(target)
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		bot.HumanoidRootPart.Anchored = true
		target.HumanoidRootPart.Anchored = true
		jumpscare:Play()
		attack:Play()
		attack.Stopped:Wait(0.3)
		bot.HumanoidRootPart.Anchored = false
		target.HumanoidRootPart.Anchored = false
	end
end

local function findTarget()
	local target = nil
	local dist = Distance or 100
	
	for index, player in pairs(game:GetService("Players"):GetPlayers()) do
		local character = player.Character
		
		if character and character:FindFirstChild("HumanoidRootPart") then
			if (bot.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude <= dist then
				dist = (bot.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude
				target = character
			end
		end
	end

	return target
end

local function followTarget()
	local function attackTarget(target)
		if (bot.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude <= AttackDistance then
			print("Target:" .. target.Name)
			killTarget(target)
			return true
		end
		return false
	end
	
	local target = findTarget()
	if target then
		if (bot.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude > PathBreakerDistance then
			local path = game:GetService("PathfindingService"):CreatePath()
			path:ComputeAsync(bot.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
			local waypoints = path:GetWaypoints()
			
			if path.Status == Enum.PathStatus.Success then
				for index, waypoint in ipairs(waypoints) do
					bot.Humanoid:MoveTo(waypoint.Position)
					
					if (bot.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude <= PathBreakerDistance then break end
					if attackTarget(target) then break end
					
					local TimeOut = bot.Humanoid.MoveToFinished:Wait()
					if not TimeOut then
						bot.Humanoid.Jump = true
						followTarget()
					end
				end
			end
		else
			bot.Humanoid:MoveTo(target.HumanoidRootPart.Position)
			attackTarget(target)
		end
		
	end
end

while wait() do
	followTarget()
end
1 Like

But,the NPC didnt move.Why?I’m still learning in scripting

Are you talking about my script or your script which isn’t working?

1 Like

It’s okay,I edit the script a little bit and now it work perfectly.Ty

This what happen before I edit yours:

18:09:53.340  Workspace.NPCs.Bot.aiScript:41: Script timeout: exhausted allowed execution time  -  Server - aiScript:27
  18:09:53.341  Stack Begin  -  Studio
  18:09:53.342  Script 'Workspace.NPCs.Bot.aiScript', Line 27 - function findTarget  -  Studio - aiScript:27
  18:09:53.343  Script 'Workspace.NPCs.Bot.aiScript', Line 41  -  Studio - aiScript:41
  18:09:53.345  Stack End  -  Studio

Maybe you can tell me how to change this script:

local teddy = script.Parent
local humanoid = teddy.Humanoid
teddy.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
	local origin = teddy.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, teddy)
	
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	
	return nearestTarget
end

local function getPath(destination)
	local PathfindingService = game:GetService("PathfindingService")
	
	local pathParams = {
		["AgentHeight"] = 15,
		["AgentRadius"] = 4,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(pathParams)
	
	path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)
	
	return path
end

local function attack(target)
	local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

	if distance > 8 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		local attackAnim = humanoid:LoadAnimation(script.Attack)
		attackAnim:Play()
		target.Humanoid.Health = 0
	end
end

local function walkTo(destination)
	
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				print("TARGET FOUND", target.Name)
				attack(target)
				break
			else
				print("Moving to ", waypoint.Position)
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while wait(0.25) do
	patrol()
end

This script is from @GnomeCode but the problem is,the aiscript from GnomeCode is needed a pathPart. I wanna make the npc follow player without pathpart and i also want the npc cant go trough wall but ignore the door.