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