#My NPC AI isn't working
97 messages · Page 1 of 1 (latest)
local teddy = script.Parent
local humanoid = teddy.Humanoid
teddy.PrimaryPart:SetNetworkOwner(nil)
local wait_time = 1
local function canSeeTarget(target)
local origin = teddy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, teddy)
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 = 40
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (teddy.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 PathfindingService = game:GetService("PathfindingService")
local pathParams = {
["AgentHeight"] = 7.037,
["AgentRadius"] = 3,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)
return path
end
local function runanim()
local RunAnim = humanoid:LoadAnimation(script.Parent.Run)
local run = 0
if run == 0 then
RunAnim:Play()
run = 1
end
RunAnim:Stop()
end
local function attack(target)
local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 6.5 then
wait_time = 0.25
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
wait_time = 1
local attackAnim = humanoid:LoadAnimation(script.Attack)
attackAnim:Play()
target.Humanoid.Health = 0
end
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
print("TARGET FOUND", target.Name)
attack(target)
break
else
print("Moving to ", waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
end
end
function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
local chosenwaypoint = waypoints[randomNum]
walkTo(chosenwaypoint)
--for i, v in pairs(workspace.waypoints:GetChildren()) do
--local randomNum = math.random(1, #v.Parent)
--walkTo(v)
--end
end
while wait(wait_time) do
patrol()
end
this is the whole script
local function attack(target)
local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 6.5 then
wait_time = 0.25
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
wait_time = 1
local attackAnim = humanoid:LoadAnimation(script.Attack)
attackAnim:Play()
target.Humanoid.Health = 0
end
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
print("TARGET FOUND", target.Name)
attack(target)
break
else
print("Moving to ", waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
end
end
function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
local chosenwaypoint = waypoints[randomNum]
walkTo(chosenwaypoint)
--for i, v in pairs(workspace.waypoints:GetChildren()) do
--local randomNum = math.random(1, #v.Parent)
--walkTo(v)
--end
end
while wait(wait_time) do
patrol()
end
Just for the code formatting ig
that's not the full script btw
Oh
yh sorry
You have a video of him like failing
yh
it did it for like the first 5 seconds
but sometimes it gets stuck infinitely
sorry for the long vid
u only need like 10 secs
yh lol
** You are now Level 1! **
local pathParams = {
["AgentHeight"] = 7.037,
["AgentRadius"] = 3,
["AgentCanJump"] = false
}
He's exited the matrix
Ig adjust agents radius
It seems like he might be catching the wall there
Increase it until it works ig
alr
Probably won't work but worth a try
The first idea that comes to your head and seems reasonable is always the worst idea
-Some Yt guy
As a pathfind algorithm I can confirm that I am stupid
Hmmmmmm🤔
ikr
Anything unique abt that wall?
Just go with it ig
If it doesn't get there in a certain amt of time maybe you can't recomputate the path
yh
Anyway the amt of times this has happened in piggy is like astoundingly large
So I would say it might be unavoidable
SimplePath is an open-source pathfinding module that gives you the ability to quickly create a pathfinding script for humanoids and non-humanoids with just a few lines of code. Pathfinding is done using Roblox’s PathfindingService. This module works by using a “repetitive” approach to pathfinding. The pathfinding agent moves a more efficient pa...
There is this tho
My friend told me abt this module
I'll try it later
Haven't tried it myself yet but it's better than the shit rblx has
if i see it doesnt work anymore
Ok
alr
Seeya
gl
** You are now Level 7! **
** You are now Level 2! **
?
Well you know how to play the animation right?
yh
alr
So like after the MoveTo statement I would play the run animation where its looped
Then I would wait for the MoveTo to be done
Then I would stop the run animation
Idk that's my logic
Ig you could also make looped false afterwards for good measure
no sorry i meant like it replays the first second of the animation meaning that it keeps playing the animation instead of letting it play out
Idk