"attempt to perform arithmetic (sub) on number and nil" occurring and I don't know why

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want the Enemy to chase after the Character using pathfinding
  2. What is the issue? Include screenshots / videos if possible!
    When ever the code run the same error comes up and I don’t know why
    this is my code
local TargetDistance = 100
local StopDistance = 5

function FindNearestPlayer()
	local PlayerList = Players:GetPlayers()
	local NearestPlayer = nil
	local distance = nil
	local direction = nil
	local EnemyChar = nil
	
	for _, Player in pairs(PlayerList) do
		local Character = Player.Character
		if Character then
			local DistanceVector = Player.Character.HumanoidRootPart.Position - HRP.Position
			if not NearestPlayer then
				NearestPlayer = Player
				EnemyChar = Player.Character
				distance = DistanceVector.Magnitude
				direction = DistanceVector.Unit
			elseif DistanceVector.Magnitude < distance then
				NearestPlayer = Player
				distance = DistanceVector.Magnitude
				direction = DistanceVector.Unit
				EnemyChar = Player.Character
			end
		end
	end
	return NearestPlayer, distance, direction, EnemyChar
end

local Path = SimplePath.new(Character)

RunService.Heartbeat:Connect(function()
	local NearestPlayer, distance, direction,  EnemyChar = FindNearestPlayer()
	if NearestPlayer.Character.HumanoidRootPart then
		if distance <= TargetDistance and distance >= StopDistance then
			local Goal = workspace.Part1
			if distance <= 30 then
				Humanoid:Move(direction)
			elseif distance > 30 then
				print(EnemyChar, NearestPlayer, EnemyChar.HumanoidRootPart, Character)
				SimplePath:Run(EnemyChar.HumanoidRootPart)	
			end
		else
			Humanoid:Move(Vector3.new())
		end
	end
end)
![image|676x35](upload://a0zyZjyfQn5AnEXCTw5zfm1nw2O.png)

The image got put into the code section for some reason.
image
Edit I found the solution I didnt put path:Run I did SimplePath:Run

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.