-
What do you want to achieve? Keep it simple and clear!
fix my npc stuttering -
What is the issue? Include screenshots / videos if possible!
it stutters when close to player
My code
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Zombie = script.Parent
for i, Part in ipairs(Zombie:GetDescendants()) do
if Part:IsA("BasePart") then
Part:SetNetworkOwner(nil)
end
end
local VisiblePoints = {}
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local path = PathfindingService:CreatePath({AgentRadius = 4.5, AgentHeight = 5, AgentCanJump = true, WaypointSpacing = 2.5})
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local destination
local function followPath(Target)
destination = Target.Position
local success, errorMessage = pcall(function()
path:ComputeAsync(character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
print("start")
for i, Point in ipairs(VisiblePoints) do
Point:Destroy()
end
VisiblePoints = {}
for i, WayPoint in ipairs(waypoints) do
local Point = Instance.new("Part", workspace)
Point.Shape = Enum.PartType.Ball
Point.Material = Enum.Material.Neon
Point.BrickColor = BrickColor:Green()
Point.Size = Vector3.new(0.5,0.5,0.5)
Point.Anchored = true
Point.CanCollide = false
Point.CanTouch = false
Point.CanQuery = false
Point.Position = WayPoint.Position
table.insert(VisiblePoints, Point)
end
if not blockedConnection then
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
print("Block")
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
followPath(destination)
end
end)
end
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
print("Move complete")
if reached and nextWaypointIndex < #waypoints and Target.Position == destination then
print("Continue")
nextWaypointIndex += 1
if waypoints[nextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
print("jump")
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
elseif not reached or Target.Position ~= destination then
print("Change")
if nextWaypointIndex < #waypoints then
nextWaypointIndex += 1
if waypoints[nextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
print("Jump")
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
print("Move")
else
local Origin = Zombie.HumanoidRootPart.Position
local Raycast = workspace:Raycast(Origin, (Target.Position - Origin).Unit * 5)
if Raycast and Raycast.Instance.Parent == Target.Parent then
print("Raycast")
humanoid:MoveTo(Target.Position)
else
print("Not raycast")
end
end
followPath(Target)
else
print("Finished")
end
end)
end
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
print("StartMove")
else
error(errorMessage)
end
end
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
wait(3)
followPath(Character.HumanoidRootPart)
end)
end)