Video
robloxapp-20240719-1753242.wmv (5.8 MB)
Here is the AI Script
local Monster = script.Parent
local humanoid = Monster.Humanoid
local pathfindingService = game:GetService(“PathfindingService”)
Monster.PrimaryPart:SetNetworkOwner(nil)
local Track = humanoid:LoadAnimation(script.WalkAnim)
Track.Looped = true
Track:Play()
local function canSeeTarget(target)
local origin = Monster.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - Monster.HumanoidRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, Monster)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 50
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (Monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
[“AgentHeight”] = 5,
[“AgentRadius”] = 4,
[“AgentCanJump”] = false
}
local path = pathfindingService:CreatePath(pathParams)
path:ComputeAsync(Monster.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
attack(target)
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.Position - (Monster.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function patrol()
local wayPoints = workspace.Waypoints:GetChildren()
local randomNum = math.random(1, #wayPoints)
walkTo(wayPoints[randomNum])
end
while wait(0.5) do
patrol()
end
Here is the Game
I just want it to chase me, and when it stops chasing, it goes back to wandering around.