You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want the Enemy to chase after the Character using pathfinding -
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)
