ahhh i feel like im going crazy trying to get this monster NPC script together and working the way i want.
the script i have made so far works, but it seems like the monster is trying to go between moving to a waypoint and moving to the target player. it walks towards the target, then turns around for a second like its going to a different waypoint, then turns back around and goes for the target again.
it also does a little circle when trying to maneuver around corners. ive made sure i set the HipHeight correctly, and even tweaked it a little to see if that was the problem. that didnt work.
do you guys see anything wrong with this script that im just over looking?
ive been working on it so long that i feel like im looking at parts that may not be the problem. I need a fresh set of eyes to take a look lol
local NPC = script.Parent
local npcRoot = NPC:WaitForChild(“HumanoidRootPart”)
local npcHumanoid = NPC:WaitForChild(“Humanoid”)
local Players = game:GetService(“Players”)
local PathfindingService = game:GetService(“PathfindingService”)
NPC.PrimaryPart:SetNetworkOwner(nil)
task.wait(3)
local function findNearestPlayer()
local nearestPlayer = nil
local shortestDistance = 100
for i, player in pairs(Players:GetPlayers()) do
if player.Character and player.Character:WaitForChild("HumanoidRootPart") then
local distance = (npcRoot.Position - player.Character.HumanoidRootPart.Position).magnitude
if distance < shortestDistance then
-- Perform a raycast to check line of sight
local origin = npcRoot.Position
local direction = (player.Character.HumanoidRootPart.Position - origin).unit * distance
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Include
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult and raycastResult.Instance:IsDescendantOf(player.Character) then
shortestDistance = distance
nearestPlayer = player
else
print("no target")
end
end
end
end
return nearestPlayer
end
local function moveToTarget(target)
local path = PathfindingService:CreatePath({
AgentRadius = 10,
AgentHeight = 10,
AgentCanJump = false,
Costs = {
Water = math.huge,
DangerZone = math.huge
}
})
path:ComputeAsync(NPC.PrimaryPart.Position, target.Character.HumanoidRootPart.Position)
if path.Status == Enum.PathStatus.Success then ------------------ i think the problem is here
for i, waypoint in pairs(path:GetWaypoints()) do
npcHumanoid:MoveTo(waypoint.Position)
npcHumanoid.MoveToFinished:Wait(1) ------------------
end
end
return path
end
while wait(0.1) do
local target = findNearestPlayer()
if target and target.Character and target.Character:FindFirstChild(“HumanoidRootPart”) then
print(target.Name)
moveToTarget(target)
end
end