My "fast" enemy slows down when it gets cloer

hes supposed to be fast
but i can outrun him ON FOOT

-- Variables -- 
local DamageSystem = require(game:GetService("ServerScriptService"):WaitForChild("DamageSystem"))
local Pathfinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

script.Parent:WaitForChild("HumanoidRootPart"):SetNetworkOwner(nil)

local path = Pathfinding:CreatePath({
	AgentHeight = 2;
	AgentRadius = 1.5;
	AgentCanJump = false;
	
	Costs = {
		Water = 100;
		DangerZone = math.huge
	}
})

local Character = script.Parent 
local humanoid = Character:WaitForChild("Humanoid")

local waypoints
local nextWaypointIndex
local reachedConnection 
local blockedConnection 

local attackdebounce = false
local attackanim = humanoid:LoadAnimation(script.Attack)

local function findTarget()
	local maxDistance = 500
	local nearestTarget 
	local distance
	for index, player in pairs(Players:GetPlayers()) do
		if player.Character then
			local target = player.Character
			distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position) .Magnitude
			
			if distance < maxDistance then
				nearestTarget = target 
				maxDistance = distance 
			end
		end
	end
	
	return nearestTarget, distance
end
local function followPath(destination)
	
	local sucess, errorMessage =pcall(function()
		path:ComputeAsync(Character.HumanoidRootPart.Position, destination)
	end)
	
	
	if sucess and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= nextWaypointIndex then 
				print("blocked")
				blockedConnection:Disconnect() 
				followPath(destination)
			end
		end)
		
		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					nextWaypointIndex += 1 
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
					humanoid.MoveToFinished:Wait()
				else

					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
		
		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		humanoid.MoveToFinished:Wait()
	else
		warn("Path not computed! :(", errorMessage)
	end
end

while wait() do 
	local target, distance = findTarget()
	if target and target:WaitForChild("Humanoid").Health > 0 then
		if distance > 1 then
			followPath(target.HumanoidRootPart.Position)
		else
			attackdebounce = true
			attackanim:Play()
			attackanim:AdjustSpeed(4)
			Character:WaitForChild("HumanoidRootPart"):WaitForChild("Swing"):Play()
			local distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position) .Magnitude
			if distance < 1.5 then

				Character:WaitForChild("HumanoidRootPart"):WaitForChild("Hit"):Play()
				DamageSystem.DamageTargetWithAttacker(1,target,Character)
				attackdebounce = false
			else
				attackdebounce = false
			end

		end

	end
end
4 Likes

what did i try?

i tried

  • removing the attacking function

  • removing pathfinding during chase

none of them worked

1 Like

Is it a server script? Might be because of that or because the logic inside the loop just takes a while. Never really encountered this since I do pathfinding on the client side usually.

1 Like

it is a server script
yes
how would i transfer the pathfinding data from the client to the server though?

This is a good video about it.

Have you tried setting the NetworkOwner of the dummy’s HumanoidRootPart to nil during the chase?

I came across that same issue, it looks like he is behind you but on the server he is with you. This is a problem from roblox’s server refresh rate

i set it to nil when the script starts
i dont think Network Ownership is the problem here

To add onto this I believe they wanted to increase it, but I might be mixing it up with the adjustable frame rate feature.

okay wait this is new


he jitters

edit: he only does this when he kills me

i might have to remake the ai from scratch :frowning:

call :SetNetworkOwner(nil) on the primary part

i did
didnt you read the script?

i haven’t noticed it mb
was looking for primary part and skipped past that

1 Like

oh, ok
yeah idk whats going on

You could get the position infront of the player and pathfind to it instead (with a raycast check to ensure the player isn’t walking into a wall)

player’s direction would be playerRoot.Velocity*2

It could be due to lag. You move on your screen before the Server sees you move, and your zombie targets where you were on the Server instead of where you are on your screen.

Try moving the path target forward in the direction of movement:

local targetRootPart = target.HumanoidRootPart
-- where the player will be 0.5 seconds from now
local targetPos = targetRootPart.Position
                + (targetRootPart.AssemblyLinearVelocity * 0.5)
followPath(targetPos)

still produces the same effect
just he can get close
attack once
and then he starts jittering again.

Maybe you could have it move directly to the player if it’s close enough instead of computing a path, avoiding that time cost:

if distance > 1 then
    local targetRootPart = target.HumanoidRootPart
    -- where the player will be 0.5 seconds from now
    local targetPos = targetRootPart.Position
                    + (targetRootPart.AssemblyLinearVelocity * 0.5)
    
    if distance < 10 then
        humanoid:MoveTo(targetPos)
    else
        followPath(targetPos)
    end
else