For loop stopping without error after an if statement

My end goal is to run a snippet of code for each player which the bool value named “safe” in their player model is set to false.

The issue here is that the while loop suddenly stops after this line of code: if plr.Character:WaitForChild('Humanoid', 1).Health >= 0 then return end without any error in console related to the script. Here’s my entire loop for context.

	wait(1)
	print('doing for loop')
	for i, plr in pairs(game:GetService("Players"):GetPlayers()) do
		print(plr.Name)
		if plr:WaitForChild('safe', 1).Value == false then
			print('not safe')
			if not plr.Character then return end
			print('has character')
			if not plr.Character:WaitForChild('Humanoid', 1) then return end
			print('has humanoid')
			if plr.Character:WaitForChild('Humanoid', 1).Health >= 0 then return end
			print('not dead')
			plr:WaitForChild('time', 1).Value += 1
			plr.Character:WaitForChild('Head'):WaitForChild('NameGUI', 1).name.Text = plr:WaitForChild('time', 1).Value
			players[plr.UserId].timeval = plr:WaitForChild('time', 1).Value
			
		end
	end
end

I’ve tried things like adding WaitForChild, separating my one if statement to find which part was stopping the while loop (I used to have all of those statements in one long one), checking to see if the humanoid even exists, etc. I’d expect an error at the very least, but it simply stops, with it’s last output being

doing for loop
cheslin23t
not safe
has character
has humanoid

The funny thing is, if one of the earlier if statement returns before it ever gets to the if statement in question (checking if the character is alive), the loop would continue running. I am looking for a working solution, but I’d also like to know why that specific line wouldn’t work correctly, or at least provide an error. Thank you.

3 Likes

Should I create a bug report for this? I seriously doubt this would be a bug, but if it wasn’t a bug, I’d imagine it would be simple enough to get at least a few solutions by now.

I noticed The code :”print(“not dead”)
But when humanoid.health >=0 it will return
If you wanna to set it to when Humanoid.health < 0 at first time

1 Like

I caught that later on, but still doesn’t explain why the loop entirely stops running. Thanks though.

return exits the function entirely. break stops the loop, and the remaining code in the block, continuing onto lines past the loop, and continue ends the current iteration of the loop and skips to the next. You are probably looking for continue, as it seems you want to loop through all the players but stop searching the player if some conditions aren’t met.

Also the specific line, you are calling return when the player has more than or 0 health, which invalidates the condition entirely as it’s always going to be above 0 or 0. I think you meant if the player had 0 health, then they would be dead, which you would then find the next player using continue.

2 Likes

Continue will stop the entire for loop. I need to run that snippet for every player with those conditions. My use of return is correct. I’m also aware about my > vs < misuse. That’s not what the question is about. My use of return is acting like a break, completely stopping the while loop, only on that condition. Thank you for your response, but that simply doesn’t explain why it stopped the loop.

1 Like

There is no need to use waitForChild. From the look of things all these things should be loaded in

I have little hard time understand this. isn’t that this topic are about? because of:

so it stop loop without error.

I am not sure how will continue stop entire loop I think @theseformasentence have clear explanation already. have you try changed return to continue before replying to that?

http://lua-users.org/wiki/ContinueProposal

No, continue doesn’t stop the for loop, while return exits the function, and therefore stops the for loop. A if—then statement isnt a function, so return skips that and goes back to the most recent function in the stack. break stops execution of the for loop. If you want to continue using return, nest the condition at the top “if plr” inside a function and pass the necessary data into the function. That way, using return exits the function and you have the option to get more data about the conditions, while remaining in the loop.

Or, just use continue to skip to the next iteration.

Edit: break will only stop the last while, for, or repeat loop, and wont stop the block above, such as the while loop you were talking about. return, no matter what, always exits the block of the latest function. return doesn’t exit the current block, such as a for loop or an if—then statement, because they aren’t functions, and you cannot execute anything past return, break, or continue only in that block.

for i=1,5 do
     if i <= 3 then continue end
     print(i) --Output: 4, 5
end 

local result = {}
for i=1,5 do
     table.insert(result,function()
          if i >= 6 then return true elseif i <= 3 then return -1 else return 1 end
          result = {} --Will never run in the function
     end)
end
print(result) --Output: {[1]=-1, [2]=-1, [3]=1, [4]=1, [5]=1, [6]=true}

result = {}
for i=1,5 do
     if i <= 3 then table.insert(result,-1) continue end --Runs once with break and return, finishes 5 iterations with continue
     if i == 4 then result = {} end --Runs once with continue, never with break or return
end
print(result) --Output: {}

They are different. You can use return in the second loop if you want data, but continue and break work in its place, you just wont get any of the other data.

@RandomPlayerOne1 I missworded what I said, but yet again, this doesn’t solve the original problem. The whole entire while loop stops.

I was trying everything I could, I had no error to deal with.

Thank you for your responses, but none has solved the actual problem.

If the loop stops, it’s because return was called somewhere. Try printing the value of each of your conditions print(1 <= 0)

The loop runs just fine with return for each if statement, it wasn’t until the if statements before the one in question were false (not running return), causing that if statement to run, and end without error. I’ve used return with the prior if statements, which worked as expected. Thank you for replying, though.

Try replacing your returns inside the if statement with a specialized error using error(" "). It would probably help trying anything to understand why your loop stops