I’m creating an AI script for my npc but when I used a while wait do loop, it makes the npc very choppy and it doesn’t run too great. So I tried using the run service and that makes it a lot less choppy but it makes the npc act funny. Here is some footage
robloxapp-20200514-1734218.wmv (4.2 MB)
(Sorry you have to download it, it wouldn’t let me upload an mp4 )
----Wait for game to load
wait()
----Variables
local run = script.Parent.Humanoid:LoadAnimation(script.Parent.Animations.Run)
local walk = script.Parent.Humanoid:LoadAnimation(script.Parent.Animations.Walk)
local service = game:GetService("PathfindingService")
local players = game.Players:GetPlayers()
local human = script.Parent.Humanoid
local hrp = script.Parent.HumanoidRootPart
----Functions
function Roam(target)
human.WalkSpeed = 5
walk:Stop()
if (target.Position - hrp.Position).Magnitude <= 70 then return end
wait(2)
local X = math.random(-20,20)
local Z = math.random(-20,20)
local path = service:CreatePath()
local goal = hrp.Position + Vector3.new(X,0,Z)
path:ComputeAsync(hrp.Position,goal)
if path.Status == Enum.PathStatus.Success then
print("Success")
local points = path:GetWaypoints()
for _, point in ipairs(points) do
if point.Action == Enum.PathWaypointAction.Jump then
human.Jump = true
print("Jump")
end
human:MoveTo(point.Position)
run:Stop()
walk:Play()
if (target.Position - hrp.Position).Magnitude <= 70 then return end
local finished = human.MoveToFinished:Wait()
if not finished then
human.Jump = true
Roam()
end
end
else
print("Path failed")
Roam()
end
end
----
function Attack(target)
human.WalkSpeed = 30
local path = service:CreatePath()
local goal = target.Position
path:ComputeAsync(hrp.Position,goal)
if path.Status == Enum.PathStatus.Success then
local points = path:GetWaypoints()
for _, point in ipairs(points) do
if point.Action == Enum.PathWaypointAction.Jump then
human.Jump = true
end
human:MoveTo(point.Position)
walk:Stop()
run:Play()
local finished = human.MoveToFinished:Wait()
if not finished then
Roam()
end
end
else
print("Chase interupted")
Roam()
end
end
----
function Scan(target)
if (target.Position - hrp.Position).Magnitude > 70 then
Roam(target)
elseif (target.Position - hrp.Position).Magnitude <= 70 then
Attack(target)
end
end
----
game:GetService("RunService").Stepped:Connect(function()
for i,p in pairs(players) do
if p and p.Character and p.Character.HumanoidRootPart then
Scan(p.Character.HumanoidRootPart)
end
wait(2)
end
end)
Here is the code to it, If anyone knows what I should use instead of the run service then please tell me! It would be greatly appreciated! Thanks! - ScriptToon