So I have a function that contains a while loop that has a pathfinding program for the AI to go towards the player. The pathfinding program works good but when the loop is suppose to break (which it does), it continues playing. The function never ends as there is a print after the function is called (the function is called once within another function) and the print is never called. Here’s the code:
local path
local waypoint
local function AttackingPlayer()
enemy.Zombie.WalkSpeed = 25
if not enemy.Sounds.SpiderSounds.IsPlaying then enemy.Sounds.SpiderSounds:Play() end
for _, track in pairs(enemy.Zombie:GetPlayingAnimationTracks()) do
track:Stop()
end
crawlingAny:Play()
while wait() do
local nrstt = selectedPlayer.Character.HumanoidRootPart
local human = enemy.Zombie
local hroot = enemy.HumanoidRootPart
if nrstt ~= nil and selectedPlayer.Character.Humanoid.Health > 0 then -- if player detected
local function checkw(t)
local ci = 3
if ci > #t then
ci = 3
end
if t[ci] == nil and ci < #t then
repeat ci = ci + 1 wait() until t[ci] ~= nil
return Vector3.new(1,0,0) + t[ci]
else
ci = 3
return t[ci]
end
end
path = pathfindingService:FindPathAsync(hroot.Position, nrstt.Position)
waypoint = path:GetWaypoints()
local connection;
local direct = Vector3.FromNormalId(Enum.NormalId.Front)
local ncf = hroot.CFrame * CFrame.new(direct)
direct = ncf.p.unit
local rootr = Ray.new(hroot.Position, direct)
local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)
if path and waypoint or checkw(waypoint) then
if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
human:MoveTo( checkw(waypoint).Position )
human.Jump = false
end
if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
connection = human.Changed:connect(function()
human.Jump = true
end)
human:MoveTo( waypoint[4].Position )
else
human.Jump = false
end
if connection then
connection:Disconnect()
end
else
for i = 3, #waypoint do
human:MoveTo( waypoint[i].Position )
end
end
path = nil
waypoint = nil
elseif nrstt == nil then
path = nil
waypoint = nil
human.MoveToFinished:Wait()
end
if selectedPlayer.Character.Humanoid.Health <= 0 then
break
end
end
end
Also just to remind the while loop does actually end but it’s as if the function just repeats even though it never finished.