#While loop ends after the 'for' loop ends

1 messages · Page 1 of 1 (latest)

spring jacinth
#
if Triggered.Value == true then
  local EnemyFolder = workspace.Ally
  local Target = GetClosetTarget(EnemyFolder, Mob)    
  if Target then
    local Distance = (Mob.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude
    if Distance > 5 then
      local center = Target.HumanoidRootPart
      local radius = 17.5
      local offset = Vector3.new(Mob.HumanoidRootPart.Position.X - center.Position.X, 0,  Mob.HumanoidRootPart.Position.Z - center.Position.Z)
      local startAngle = math.atan2(offset.Z, offset.X)
      local angleOffset = math.rad(90)
      local endAngle = startAngle + angleOffset
      local steps = 20
      for i = 0, steps do
      if Distance < 5 then
        break
      end
      local t = i / steps
      local angle = startAngle + (endAngle - startAngle) * t
      local x = center.Position.X + radius * math.cos(angle)
      local z = center.Position.Z + radius * math.sin(angle)
      repeat
      if Distance < 5 then
        break
      end
      Mob.Humanoid:MoveTo(Vector3.new(x, Mob.HumanoidRootPart.Position.Y, z))
      task.wait()
      until Mob.Humanoid.MoveToFinished:Wait()
     end
end)```

Basically the while task.wait() do stop functioning after for i = 0, steps do ended
silent jolt
#

this is a newb trap Mob.Humanoid.MoveToFinished:Wait() and this is just dum lua task.wait() until Mob.Humanoid.MoveToFinished:Wait() because you now have a 1 frame window where this function can get stuck waiting indefinitely if the movetofinished fires before the thread resumes.
also looks like you didn't show the whole code because this end) doesn't pair with anything, in fact in the position it is in, this code should error.
in short, do pathfinding properly, here's a guide: https://create.roblox.com/docs/characters/pathfinding

Pathfinding is the process of moving a character along a logical path to reach a destination.

spring jacinth
#

And it's not pathfind

silent jolt
# spring jacinth No error. Just while task.wait() stops

setting aside the fact that you aren't showing the whole code, this is not going to just randomly decide to stop randomly while task.wait() do without some error or a return or break, and there's no return or break for that loop in the code you did show.

spring jacinth
#

local Mob = Model
local Parry = Model:FindFirstChild("Parry")or Instance.new("BoolValue", "Parry", Mob)

local Triggered = Mob:FindFirstChild("Triggered") or Instance.new("BoolValue", "Triggered", Mob)

local Humanoid = Mob:FindFirstChild("Humanoid")
#

adding this on top is the whole code.MonsterFolder.ChildAdded:Connect(function(Model)

silent jolt
spring jacinth
#

Discord isn't letting me send the full code

#

I'll just seperate it

#
    local Mob = Model
    local Parry = Model:FindFirstChild("Parry")or Instance.new("BoolValue", "Parry", Mob)

    local Triggered = Mob:FindFirstChild("Triggered") or Instance.new("BoolValue", "Triggered", Mob)

    local Humanoid = Mob:FindFirstChild("Humanoid")
    
    while task.wait() do
        if Triggered.Value == true then
            local EnemyFolder = workspace.Ally
            local Target = GetClosetTarget(EnemyFolder, Mob)
            
            if Target then
                local Distance = (Mob.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude
                if Distance > 5 then
                    local center = Target.HumanoidRootPart
                    local radius = 17.5

                    local offset = Vector3.new(Mob.HumanoidRootPart.Position.X - center.Position.X, 0,  Mob.HumanoidRootPart.Position.Z - center.Position.Z)
                    local startAngle = math.atan2(offset.Z, offset.X)```
#
                    local endAngle = startAngle + angleOffset

                    local steps = 20
                    for i = 0, steps do
                        if Distance < 5 then
                            break
                        end
                        
                        local t = i / steps
                        local angle = startAngle + (endAngle - startAngle) * t

                        local x = center.Position.X + radius * math.cos(angle)
                        local z = center.Position.Z + radius * math.sin(angle)

                        repeat
                            if Distance < 5 then
                                break
                            end
                            Mob.Humanoid:MoveTo(Vector3.new(x, Mob.HumanoidRootPart.Position.Y, z))
                            task.wait()
                            
                        until Mob.Humanoid.MoveToFinished:Wait()
                    end
                    
                else
                    repeat
                        task.wait()
                        local Distance = Mob.HumanoidRootPart.CFrame.LookVector
                        local Position = CFrame.new((Mob.HumanoidRootPart.CFrame * -Distance)).Position
                        Mob.Humanoid:MoveTo(Position)
                    until Mob.Humanoid.MoveToFinished:Wait()
                end
            end
        end
    end
end)```
#

What this suppose to do is get the Center arc. Then tell the NPC to walk along it

silent jolt
#

yea i noticed the circle walking

#

it's a form of pathfinding you still need to handle movetofinished correctly

#

anyway the while loop is likely still running because there's no return or break. this condition is prob false if Triggered.Value == true then could be this one also if Target then

spring jacinth
#

Triggered fire in a event. Basically if your character (Client Side) is close enough to the Mob(NPC). It will fire a event that flips the Triggered value into true

molten pathBOT
#

studio** You are now Level 1! **studio

silent jolt
#

i suggest adding prints etc to prove things are running and working as expected, fix how you're using the newb trap of humanoid.movetofinished:wait() etc and you should be good.

spring jacinth
#

This is a print under while loop. Running normally

#

and this is after triggering the NPC. It ended at 2044. print one more time and completly stop

#

It's similar to a break. Since I add print("test") on every loop with wait()

silent jolt
#

you have no break or return hitting that while loop bruh

spring jacinth
#

Exactly

#

The code run through everything on the last loop

#

then stops

spring jacinth
silent jolt
#

cmon bruhhhhhh