For loop not working when in combination with an IF statement

Hello. I noticed that my script is seemingly breaking when attached with an if statement. My goal is to read if an argument is a model with a humanoid (like the player) and detect when something isn’t.

Issue: Once it detects something else other than what meets the requirements it just stops.

function GridModule.InsertToGrid(UnitsToInsert)
	for index, unit in pairs(UnitsToInsert) do
		print(unit)
		if unit:IsA("Model") and unit:FindFirstChild("Humanoid") then
			print("Valid!")
		else
			warn("Not Valid!")
		end
	end
end

Here are the arguments being passed through:

gridModule.InsertToGrid({player.Character, "Jim", "Steve", "Bill", "John"})

Do you get any errors, also what does the print statement that is supposed to show the unit print for index 1 (the character)?

Also, are you saying that if the unit is not a model and doesn’t have a humanoid then “Not valid” isn’t printed?

Oddly enough, no errors.
It’s supposed to show the value (in this case, the name). For example, Steve, bill, etc.

Yeah but what does it print for the player.Character when you run the script?

Oh, it simply prints my name. For example, it appears as: “Dreisang”

image
Here is an example as to what it does. It stops at “Jim” in this regard, the second argument.

1 Like

Response for Edit: Yes. It is supposed to check if it is a model and has a humanoid. If it does not, then it is supposed to print “Not Valid”, which doesn’t occur.

1 Like

Okay we can try this:

  • Add a print statement outside the for in loop
  • Add a print statement beneath the if statement

The content of these can be whatever, we just need to determine where the script stops, and if it stops at the else statement or not.

Once done post the result.

Here is the formatting of the code with the print statements.

	print("Start!")
	for index, unit in pairs(UnitsToInsert) do
		print(unit)
		if unit:IsA("Model") and unit:FindFirstChild("Humanoid") then
			print("Valid!")
		else
			print("Not true!")
		end
		print("Stopped!")
	end
end

Here is the output:
image

Can you also add a statement beneath the loop?

It is stopping at the second loop (where it prints “Jim”), oddly enough. If I replace “Jim” with something that meets the requirements, it works until it finds something that does not.

I need you to add the statement beneath the for-in loop to check if the loop finishes. I doubt it does but it’s best to double-check.

I just want to see it to find out if the function is terminating or the loop.

Actually… no. It STOPS.

It does not print “Stops”. It is likely because it is terminating for some cause.

Wait, so maybe the function is terminating? That’s not what I expected.

I did some moving around in the script where it is starting and, as odd as it sounds, I do actually get an error. Here’s the code for when the function is playing:

repRemotes.BattleBegin.OnServerEvent:Connect(function(player)
	local WaitForLoad = coroutine.create(function()
		print("Waiting...")
		coroutine.yield()
		print("Grid Created!")
	end)
	game.ServerStorage.Remotes.GridCreated.Event:Connect(function()
		coroutine.resume(WaitForLoad)
	end)
	gridModule.CreateGrid(workspace.TestModel)
	coroutine.resume(WaitForLoad)
	print("Running!")
	wait(5) -- for testing purposes for this case.
	gridModule.InsertToGrid({player.Character, "Jim", "Steve", "Bill", "John"})
end)

Interestingly enough, I DO get an error when I wait.
image

It is coming from this line here:

if unit:IsA("Model") and unit:FindFirstChild("Humanoid") then
			print("Valid!")
		end

It’s the if statements. Maybe split them into separate statements? I have honestly no idea what to do.

This would be so much easier if I had studio open.

No luck, unfortunately. It’s good that we found that it is indeed terminating. I’ll try a few different things. I appreciate your attempt at helping!

Maybe its something to do with trying to run :IsA or :FindFirstChild on a string value. Its not an instance, those functions are to be used on an instance.(I think, maybe)

IDK why it wouldn’t error faster if that were the case though, strange.

Good point. It works just fine when it detects a humanoid, only stopping if it doesn’t.