AI is super delayed tracking in map, less delayed trackingon baseplate, not delayed tracking at all in seperate game

All I want/need is my AI to track the player, I don’t know if its a scripting issue (I believe it is not) but rather I believe it is a issue with the server or something. I am using the SimplePath module for the pathfinding service so it could be related to that.

I am in dire need of help, I kinda need to fix this by tonight so if anyone knows how to help please help.

the following two clips show the issue, on the baseplate the AI is still delayed with tracking but not super delayed. On the map it is so delayed it is basically unusable. It might be an issue with the NavMesh, but I already checked and it looked fine.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Module = require(ReplicatedStorage.SimplePath)
local Players = game:GetService("Players")
local AI = script.Parent
local Target = nil
local Path = Module.new(AI)
local MaxDistance = math.huge
local Track = AI.Humanoid.Animator:LoadAnimation(script.Attack)
local ScreamTrack = AI.Humanoid.Animator:LoadAnimation(script.Scream)
local TimeDB = 0.4
local AttackDB = false
local Damage = 75
local Raging = false
Path.Visualize = true

local function playSound(sound : Sound, SoundPart)
	local Clone = sound:Clone()
	Clone.Parent = SoundPart
	Clone:Play()
	game:GetService("Debris"):AddItem(Clone, Clone.TimeLength)
end

Path.Blocked:Connect(function()
	if Target ~= nil then
		Path:Run(Target.HumanoidRootPart)
	end
end)

Path.WaypointReached:Connect(function()
	if Target ~= nil then
		Path:Run(Target.HumanoidRootPart)
	end
end)

Path.Error:Connect(function(errorType)
	if Target ~= nil then
		Path:Run(Target.HumanoidRootPart)
	end
end)

local function SearchForClosestTarget()
	local nearestTarget
	local MaxDistance = MaxDistance

	for index, player in pairs(Players:GetPlayers()) 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 IfCloseEnough(Target)
	if Target:FindFirstChild("HumanoidRootPart") then
		local distance = (AI.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude

		if distance < 4 then
			if AttackDB == false then
				AttackDB = true
				local Humanoid = Target.Humanoid
				Track:Play()
				Humanoid:TakeDamage(Damage)
				wait(TimeDB)
				AttackDB = false
			end
		end
	end
end

local function visualizeRaycast(origin, destination)
	local Part = Instance.new("Part")
	Part.Color = Color3.fromRGB(138, 255, 65)
	Part.CanCollide = false
	Part.Anchored = true
	
	Part.Size = Vector3.new(0.2, 0.2, (destination - origin).Magnitude + 1)
	Part.CFrame = CFrame.lookAt((origin + destination) / 2, destination)
	
	Part.Parent = workspace
	
	game:GetService("Debris"):AddItem(Part, 5)
end

local function checkIfRage(Target)
	local Origin = AI.Head.Position
	local Direction = (Target.Head.Position - Origin).Unit * 500
	local RayParams = RaycastParams.new()
	RayParams.FilterType = Enum.RaycastFilterType.Exclude
	local Raycast = workspace:Raycast(Origin, Direction, RayParams)
	
	if Raycast then
		if Players:GetPlayerFromCharacter(Raycast.Instance.Parent) ~= nil or Players:GetPlayerFromCharacter(Raycast.Instance.Parent.Parent) ~= nil then
			print("raging")
			Raging = true
			AI.HumanoidRootPart.Anchored = true
			ScreamTrack:Play()
			playSound(script.Sounds[math.random(1, #script.Sounds:GetChildren())], AI.Head)
			ReplicatedStorage.Events.ScreenShake:FireAllClients(true)
			ScreamTrack.Stopped:Wait()
			ReplicatedStorage.Events.ScreenShake:FireAllClients(false)
			AI.HumanoidRootPart.Anchored = false
			AI.Humanoid.WalkSpeed += 5
		end
	end
end



while wait(0.001) do
	Target = SearchForClosestTarget()
	
	if Target ~= nil then
		if Target:FindFirstChild("HumanoidRootPart") then
			Path:Run(Target.HumanoidRootPart)
			if Raging == false then
				checkIfRage(Target)
			end
			IfCloseEnough(Target)
		end
	end
end

clip 1

clip 2

3 Likes

The first clip is because it’s tracking where you are on the server. Your server character is always a little behind. If you make it print when it’s touching or test with 2 players. It will be printing and the other player will see the monster touching you.

I have no idea with the second clip.

1 Like

Ah thanks for helping with the first part. Although the first clip is not the real issue, i really need to find out what is happening in the actual map.

1 Like

Is the map made of meshes? Also maybe it’s found you then can’t find you so it’s going to the last known location. Try adding prints everywhere.

1 Like

It looks like the entire script is more delayed in the actual map, the map has mesh wall but it has part flooring. Terrain is scattered around the map too.

1 Like

Also its not going to the last known location, the entire tracking isnt updating. It is supposed to update every time the ai moves, but instead it takes like an entire 3 seconds to update.

1 Like

I think a third party may be interfering. Is it just when it’s in the map, or when the map is loaded.

Also is it just a studio issue? Or does this happen in Roblox servers.

1 Like

When its in the map. Also before in game it would work fine but not in studio but now its happeneing both times.

1 Like

You don’t need to update the player tracking every second. You should:

  1. Check for closest player
  2. Check if line of sight
    a. true: simply walk towards target pos
    b. false: pathfind
1 Like

I’ll try that.

CHARRRRRRRRRRRRRRRRR

Although that helped, it still starts glitching out as soon as your out of his line of site because he starts using pathfinding again.

You should pathfind once, and then wait for him to get to the goal (whilest doing line of sight checks).

if that’s too low resolution, try pathfinding whenever he reaches a node.