I don't understand why my for loop doesn't work

Today I was writing a for loop for my boat script that makes you sit on a seat that is currently free. However my script thinks there is only one seat.

My code:

enterBoat.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild("Humanoid") ~= nil then
		local humanoid = touch.Parent:FindFirstChild("Humanoid")

		for _, seat in pairs(boat2.Seats:GetChildren()) do
			if seat.Occupant == nil then
				humanoid.Parent:FindFirstChild("HumanoidRootPart").Position = seat.Position
				seat:Sit(humanoid)
				ShowBoatUIEvent:FireClient(Players:GetPlayerFromCharacter(humanoid.Parent), true)
				seat.Changed:Connect(function(property)
					if property == "Occupant" and not seat.Occupant then
						humanoid.Parent:FindFirstChild("HumanoidRootPart").Position = boatExit.Position
						ShowBoatUIEvent:FireClient(Players:GetPlayerFromCharacter(humanoid.Parent), false)
					end
				end)
				break
			end
		end
	end
end)

It’s just a for loop iterating over the seats and making you sit on the first free one.
I have checked my code many times and I couldn’t find anything. Can you tell me why my loop doesn’t work thx?

1 Like

There may be a couple issues. One, there may actually only be one seat in the boat (check that it’s not embedded in something else like another model)

Two, perhaps your seats somehow got an Occupant (that doesn’t make too much sense, but…)

Number three, you’re breaking the loop.
The “break” right after the end) will make your loop stop.

I tried removing break but still same behaviour.

You have to move the break into the if seat.Occupant == nil statement. Nvm it’s i n the statement, didn’t see that lol

1 Like

its in there already
pasodjoajdapsdjasop

I think i read somewhere that break would break the current loop, but not stop the next loops.

after the seat:Sit(humanoid) can you add a print(humanoid.SeatPart) to confirm the Sit actually worked.

Could you implement a simple count (replacing _ with a variable, then printing the variable) right after your loop begins?

Much easier to debug when we know where the bug itself is actually coming from.