local monster = workspace:WaitForChild("Monster")
local humanoid = monster:WaitForChild("Humanoid")
local rootPart = monster:WaitForChild("HumanoidRootPart")
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local FOLLOW_DISTANCE = 100 -- Range to detect players
local WANDER_RADIUS = 50 -- How far the monster can wander
local PATH_RECALC_TIME = 2
local currentPath = nil
local targetPlayer = nil
local lastPathTime = 0
-- Utility function to get a random position near the monster
local function getRandomWanderPosition()
local randomOffset = Vector3.new(
math.random(-WANDER_RADIUS, WANDER_RADIUS),
0,
math.random(-WANDER_RADIUS, WANDER_RADIUS)
)
local origin = rootPart.Position + randomOffset
local ray = Ray.new(origin + Vector3.new(0, 10, 0), Vector3.new(0, -20, 0))
local part, position = workspace:FindPartOnRay(ray, monster)
if part then
return position
else
return rootPart.Position
end
end