Hello! I Am Making A Horror Game Monster And There Is A Glitch(?) That Is A Bit Annoying Where The Monster Just Starts Stuttering And Not Chasing Smoothly, I Cant Really Explain More So I’ll Just Send A Video.
Video Of The Glitch :
AI Script :
local VDI = script.Parent
local Humanoid = VDI.Humanoid
local PathFindingService = game:GetService("PathfindingService")
VDI.PrimaryPart:SetNetworkOwner(nil)
local timeout = 0
local maxTimeout = 100 --How many times it checks before deciding to proceed anyways
local nextPointDistance = 2 --How far VDI has to be before going to next point
local function CanSeeTarget(Target)
local origin = VDI.HumanoidRootPart.Position
local direction = (Target.HumanoidRootPart.Position - VDI.HumanoidRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, VDI)
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 = 75
local NearestTarget = nil
for index, player in pairs(Players) do
if player.Character then
local Target = player.Character
local Distance = (VDI.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 PathParms = {
["AgentHeight"] = 8,
["AgentRadius"] = 6,
["AgentCanJump"] = false
}
local Path = PathFindingService:CreatePath(PathParms)
Path:ComputeAsync(VDI.HumanoidRootPart.Position, destination.Position)
return Path
end
local function Attack(Target)
local Distance = (VDI.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude
if Distance > 8 then
Humanoid:MoveTo(Target.HumanoidRootPart.Position)
Humanoid.WalkSpeed = 32
else
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
Attack(Target)
break
else
Humanoid.WalkSpeed = 16
Humanoid:MoveTo(waypoint.Position)
repeat
task.wait(0.05) --You can change this to whatever to increase or decrease checking speed
timeout += 1
until timeout >= maxTimeout or (VDI.PrimaryPart.Position - Vector3.new(0, 5, 0) - waypoint.Position).Magnitude <= nextPointDistance
timeout = 0
end
end
else
Humanoid:MoveTo(destination.Position - (VDI.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 task.wait(0.5) do
Patrol()
end