So I have an NPC that uses pathfinding to get around objects. Now this is the problem: When I test the game using Run, the NPC is perfect and never gets stuck.
However, If I play test the game using the player, the NPC gets stuck on stuff.
script:
local pathfindingService = game:GetService("PathfindingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local foundtarget = nil
local targetpos = nil
local humanoid = script.Parent.Humanoid
local humanoidrootpart = script.Parent.HumanoidRootPart
local body = script.Parent:FindFirstChild("Torso")
local stunned = script.Parent.Stunned
local destination = game.Workspace.CampingPath:GetChildren()
local Cooldown = 1
local HitDebounce = false
local MaxDistance = 5
local SwingSounds = Char.AttackZone.SwingSounds:GetChildren()
local HitSounds = Char.AttackZone.HitSounds:GetChildren()
local IdleAnim = humanoid.Animator:LoadAnimation(script.Parent.Idle)
local WalkAnim = humanoid.Animator:LoadAnimation(script.Parent.Walk)
local RunAnim = humanoid.Animator:LoadAnimation(script.Parent.Run)
local Attack1Anim = humanoid.Animator:LoadAnimation(script.Parent.Attack)
local Attack2Anim = humanoid.Animator:LoadAnimation(script.Parent.Attack2)
Attack1Anim.Stopped:Connect(function()
newHitbox:HitStop()
end)
Attack2Anim.Stopped:Connect(function()
newHitbox:HitStop()
end)
local blockedConnection
humanoidrootpart:SetNetworkOwner(nil)
local points = {}
for _,part in ipairs(destination) do
table.insert(points,part)
task.wait()
end
foundtarget = nil
function walkRandomly()
foundtarget = nil
local point = points[math.random(1,#points)]
local target = FindTarget()
local path = game:GetService("PathfindingService"):CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true
})
path:ComputeAsync(humanoidrootpart.Position, point.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
if foundtarget == nil then
print("DEBUG: WALKING RANDOMLY TO WAYPOINT")
humanoid:MoveTo(waypoint.Position)
path.Blocked:Connect(function()
print("DEBUG: PATH BLOCKED (WALKRANDOMLY)")
main()
end)
if waypoint.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
if foundtarget == 1 then path:Destroy() break end
humanoid.MoveToFinished:Wait()
FindTarget()
end
end
--[[
local path = game:GetService("PathfindingService"):CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false
})
--]]
function main()
local target = FindTarget()
if target and foundtarget == 1 then
print(target)
local Player = game.Players:GetPlayerFromCharacter(target.Parent)
humanoid.WalkSpeed = 18
findPath(target)
else
foundtarget = nil
humanoid.WalkSpeed = 7
walkRandomly()
end
end
humanoid.Running:Connect(function(speed)
if speed > 17 then
IdleAnim:Stop()
WalkAnim:Stop()
RunAnim:Play()
elseif speed > 4 and speed < 17 then
IdleAnim:Stop()
WalkAnim:Play()
RunAnim:Stop()
elseif speed <= 4 then
IdleAnim:Play()
WalkAnim:Stop()
RunAnim:Stop()
end
end)
while RunService.Heartbeat:Wait() do
main()
end
some code removed due to being irrelevant at this time