Monster AI help

Hello, I have a monster AI for a game that a few friends and I are working on. the ai works great however when the monster jumpscares you after the sound plays and it kills you the monster just locks in place and can’t even kill you anymore. It just freezes in place and doesn’t do anything.
How could I fix this?
Any help is amazing!

local AI = script.Parent
local Humanoid = AI.Humanoid
local PathfindingService = game:GetService("PathfindingService")
AI.PrimaryPart:SetNetworkOwner(nil)

local function FindTarget()
	local Players = game.Players:GetPlayers()
	local MaxDistance = 40
	local NearestTarget = nil
	
	for index, player in pairs(Players) do
		if player.Character then
			local Target = player.Character
			local Distance = (AI.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude
			
			if Distance < MaxDistance then
				NearestTarget = Target
				MaxDistance = Distance
			end
		end
	end
	
	return NearestTarget
end

local function GetPath(destonation)
	local PathParams = {
		["AgentHeight"] = 11,
		["AgentRadius"] = 5.5,
		["AgentCanJump"] = true
	}
	local Path = PathfindingService:CreatePath(PathParams)

	Path:ComputeAsync(AI.HumanoidRootPart.Position, destonation.Position)
	
	return Path
end

local function Attack(Target)
	local Distance = (AI.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude
	
	if Distance > 3 then	
		Humanoid:MoveTo(Target.HumanoidRootPart.Position)
	else
		local PlayerDeath = game.ReplicatedStorage:WaitForChild("PlayerDeath")
		local player = game.Players:GetPlayerFromCharacter(Target)
		PlayerDeath:FireClient(player, AI)
		local AttackAnim = Humanoid:LoadAnimation(script.Attack)
		AttackAnim:Play()
		wait(0.5)
		Target.Humanoid.Health = -5
	end
end

local function WalkTo(destonation)
	local Path = GetPath(destonation)
	
	if Path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(Path:GetWaypoints()) do
		local Target = FindTarget()
		if Target and Target.Humanoid.Health > 1 then
			print("TARGET FOUND", Target.Name)
				Attack(Target)
				break
		else
			Humanoid:MoveTo(waypoint.Position)
			Humanoid.MoveToFinished:Wait()
		end
		end
	else
		Humanoid:MoveTo(destonation.Position - (AI.HumanoidRootPart.CFrame.LookVector * 10))
	end	
end

local function Patrol()
	local WayPoints = workspace.WayPoints:GetChildren()
	local RandomNum = math.random(1, #WayPoints)
	WalkTo(WayPoints[RandomNum])
end

while wait(0.1) do
	Patrol()
end
3 Likes

Try adding print statements to see where your code stops working.

Change the while-loop for a RunService connection.

local RunService = game:GetService('RunService')

local TimePassed = 0
RunService.Stepped:Connect(function(t, delta)
	TimePassed = (TimePassed + delta >= 0.1) and 0 or TimePassed + delta
	
	if TimePassed == 0 then
		Patrol()
	end
end)

In case you’re unfamiliar with ternary statements;

TimePassed = (TimePassed + delta >= 0.1) and 0 or TimePassed + delta

is the same as

if TimePassed + delta >= 0.1 then
	TimePassed = 0
else
	TimePassed += delta
end

You may need to add an extra check to see if the AI is currently patrolling or not. A simple boolean patrolling will do

1 Like

I have zero clue what any of that means lol

Try printing values in Patrol function print(“Patrol”) at start of it and do that for each function, this will allow us to see where the issue really is…
If neither of functions stop printing, that’s the issue with script which is on the client side.

Add a variable for RunService at the top of your code, and a boolean called patrolling

local RunService = game:GetService('RunService')
local patrolling = false

Add this line in your Patrol function after WalkTo(WayPoints[RandomNum]):

patrolling = false

Then replace this code at the end of your script

with this:

local TimePassed = 0
RunService.Stepped:Connect(function(t, delta)
	TimePassed = (TimePassed + delta >= 0.1) and 0 or TimePassed + delta

	if TimePassed == 0 and not patrolling then
		patrolling = true
		Patrol()
	end
end)

Nope! the same thing happens. After he kills you he just locks in place unable to do anything