I’m a bit rusty with coding, so I’m posting here.
I want to continue my for loop when a bool value is set to true, but continue must be directly under the for loop.
I need it to still be in the event because I still need it to work when the other lines are running.
for i, v in pairs(pathpoints) do
inunderground.Changed:Connect(function(bool)
if bool == true then
continue --Want to keep here
end
end)
hum:MoveTo(v.Position, v) --Don't touch from this line
hum.MoveToFinished:Wait()
if (hrp.Position - v.Position).Magnitude > 1 then
hum:MoveTo(v.Position, v)
hum.MoveToFinished:Wait()
end --Until here
end
for i, v in pairs(pathpoints) do
if inunderground.Value == true then
continue
end
hum:MoveTo(v.Position, v) --Don't touch from this line
hum.MoveToFinished:Wait()
if (hrp.Position - v.Position).Magnitude > 1 then
hum:MoveTo(v.Position, v)
hum.MoveToFinished:Wait()
end --Until here
end
I forgot to put the full story here:
I put the event because I need it to fire even when the other lines are playing, with your code it only works at the start. How would I achieve what I want?
--At least, this was my plan. If anyone could come up with a similar idea then that would be nice :)
--event
--if ___ then
--continue (won't work because it's inside an event)
--end
--end)
for i, v in pairs(pathpoints) do
inunderground.Changed:Connect(function(bool)
if bool == true then
else
break
end
end)
hum:MoveTo(v.Position, v) --Don't touch from this line
hum.MoveToFinished:Wait()
if (hrp.Position - v.Position).Magnitude > 1 then
hum:MoveTo(v.Position, v)
hum.MoveToFinished:Wait()
end --Until here
end
Edit: and get rid of the event if possible. i don’t know how your code works, but it feels wonky to put an event in a for loop? like this would be better:
for i, v in pairs(pathpoints) do
if bool == true then
else
break
end
hum:MoveTo(v.Position, v) --Don't touch from this line
hum.MoveToFinished:Wait()
if (hrp.Position - v.Position).Magnitude > 1 then
hum:MoveTo(v.Position, v)
hum.MoveToFinished:Wait()
end
end
Your code is synchronous which means the it will only do other tasks after completing the for loop, i.e one task at a time can only be achieved. If you do want multi-threading, where two bits of code run on seperate threads independent of each other. Use task.spawn or coroutines.