For / in pairs question

Hello guys, I’m trying to make a system that when a player is seated on a seat and the Owner of the car despawn the car, then the player forced to get out, and all is good at this point, but I have a problem: I need that the script runs only if at least one of the seat is occupated, but I don’t know how to make it, below there’s the script

for i,v in pairs(plrCars[plr]:GetDescendants()) do
				if v:IsA("Seat") then
					if not v.Name == "VehicleSeat" then
					 local player = game.Players:GetPlayerFromCharacter(v.Occupant.Parent)
					 local Character = player.Character
					 local AnimationTracks = player.Character.Humanoid:GetPlayingAnimationTracks()
					 for i, v in pairs(AnimationTracks) do
						 v:Stop()
					 end
					 for i,v in pairs(Character:GetDescendants()) do 
						 if v:IsA("BasePart") then 
							 v.Massless = false
						 end
					 end
					 for i,v in pairs(Character:GetDescendants()) do 
						 if v:IsA("BasePart") or v:IsA("Decal") then 
							 v.Transparency = 0
							 Character:FindFirstChild("HumanoidRootPart").Transparency = 1
						 end
					 end
					    v.Disabled = true
				    end	
			 	end
			end

Is that the full script? Or is there another script? Or are there more variables at the top?

You can check the changes in each of the seats and turn on this script, if the owner has started the car, then activate it. However, in order to do this, I need to know the location of the scripts and parts in the car.

To detect when the Occupancy changes use:

v:GetPropertyChangedSignal("Occupant"):Connect(function()
    print("kick plr outta seat")
end

this is a part of the script but the rest of the script isn’t important, so u can consider this as a single script

well the problem is that the script is inside the ServerScriptService, and I have to use this for all the vehicle, this is the reason why I’m using this kind of script to get the seats

Did you try this:

for i,v in pairs(plrCars[plr]:GetDescendants()) do
				if v:IsA("Seat") then
                    v:GetPropertyChangedSignal("Occupant"):Connect(function()
					    if not v.Name == "VehicleSeat" then
					        local player = game.Players:GetPlayerFromCharacter(v.Occupant.Parent)
					        local Character = player.Character
					        local AnimationTracks = player.Character.Humanoid:GetPlayingAnimationTracks()
					        for i, v in pairs(AnimationTracks) do
						        v:Stop()
					        end
					         for i,v in pairs(Character:GetDescendants()) do 
						         if v:IsA("BasePart") then 
							         v.Massless = false
						         end
					         end
					         for i,v in pairs(Character:GetDescendants()) do 
						         if v:IsA("BasePart") or v:IsA("Decal") then 
							         v.Transparency = 0
							         Character:FindFirstChild("HumanoidRootPart").Transparency = 1
						         end
					         end
					         v.Disabled = true
				           end	
                        end
			 	    end
			    end

no it doesn’t work, cause the Change signal works but the game still searches for all the seats occupied

Then you need to add a break statement somewhere in the loop where you intend for it to terminate.

for i = 1, 5 do
	print(i)
	if i % 3 == 0 then break end
end

--1
--2
--3
1 Like