Iterate Looping through parts causes lag spikes

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Im making a train switch scripts that loops through parts in a model to change their transparency and
    cancollide values

  2. What is the issue? Include screenshots / videos if possible!
    whenever the platform attribution function starts there is a huge lag spikes and the script times out
    surely due to the models having more than 50 parts

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

--Points layout instructions--
-- Place the S2 on the track with the A parts, and the S1 on the track with the B parts. 
-- Each route with a set of points is automatically decided.

local s1 = script.Parent.S1
local s2 = script.Parent.S2
local s3 = script.Parent.S3
local s4 = script.Parent.S4
local s5 = script.Parent.S5
local s6 = script.Parent.S6
local s7 = script.Parent.S7
local transparency = 0.1

local occupancy = script.Parent.Occupancy
local aparts = script.Parent.AParts:GetChildren()
local bparts = script.Parent.BParts:GetChildren()
local cparts = script.Parent.CParts:GetChildren()
local dparts = script.Parent.DParts:GetChildren()
local eparts = script.Parent.EParts:GetChildren()
local fparts = script.Parent.FParts:GetChildren()
local gparts = script.Parent.GParts:GetChildren()
local hparts = script.Parent.HParts:GetChildren()
local iparts = script.Parent.IParts:GetChildren()

--//General functions
function Switch1()
	for i,v in pairs(aparts) do
		v.CanCollide = true
		v.Transparency = 0
	for i,v in pairs(bparts) do
		v.CanCollide = false
		v.Transparency = transparency
	for i,v in pairs(cparts) do
	    v.CanCollide = false
		v.Transparency = transparency
	for i,v in pairs(dparts) do
		v.CanCollide = false
		v.Transparency = transparency
	for i,v in pairs(eparts) do
		v.CanCollide = false
		v.Transparency = transparency
	for i,v in pairs(fparts) do
		v.CanCollide = false
		v.Transparency = transparency
			            end
		            end
	            end
			end
		end
	end
end

task.wait(2)

function Switch2()
	for i,v in pairs(aparts) do
		v.CanCollide = false
		v.Transparency = transparency
		for i,v in pairs(bparts) do
			v.CanCollide = true
			v.Transparency = 0
			for i,v in pairs(cparts) do
				v.CanCollide = false
				v.Transparency = transparency
				for i,v in pairs(dparts) do
					v.CanCollide = false
					v.Transparency = transparency
					for i,v in pairs(eparts) do
						v.CanCollide = false
						v.Transparency = transparency
						for i,v in pairs(fparts) do
							v.CanCollide = false
							v.Transparency = transparency
						end
					end
				end
			end
		end
	end
end

task.wait(2)

function Switch3()
	for i,v in pairs(aparts) do
		v.CanCollide = false
		v.Transparency = transparency
		for i,v in pairs(bparts) do
			v.CanCollide = false
			v.Transparency = transparency
			for i,v in pairs(cparts) do
				v.CanCollide = true
				v.Transparency = 0
				for i,v in pairs(dparts) do
					v.CanCollide = false
					v.Transparency = transparency
					for i,v in pairs(eparts) do
						v.CanCollide = false
						v.Transparency = transparency
						for i,v in pairs(fparts) do
							v.CanCollide = false
							v.Transparency = transparency
						end
					end
				end
			end
		end
	end
end

task.wait(2)

function Switch4()
	for i,v in pairs(aparts) do
		v.CanCollide = false
		v.Transparency = transparency
		for i,v in pairs(bparts) do
			v.CanCollide = false
			v.Transparency = transparency
			for i,v in pairs(cparts) do
				v.CanCollide = false
				v.Transparency = transparency
				for i,v in pairs(dparts) do
					v.CanCollide = true
					v.Transparency = 0
					for i,v in pairs(eparts) do
						v.CanCollide = false
						v.Transparency = transparency
						for i,v in pairs(fparts) do
							v.CanCollide = false
							v.Transparency = transparency
						end
					end
				end
			end
		end
	end
end

task.wait(2)

function Switch5()
	for i,v in pairs(aparts) do
		v.CanCollide = false
		v.Transparency = transparency
		for i,v in pairs(bparts) do
			v.CanCollide = false
			v.Transparency = transparency
			for i,v in pairs(cparts) do
				v.CanCollide = false
				v.Transparency = transparency
				for i,v in pairs(dparts) do
					v.CanCollide = false
					v.Transparency = transparency
					for i,v in pairs(eparts) do
						v.CanCollide = true
						v.Transparency = 0
						for i,v in pairs(fparts) do
							v.CanCollide = false
							v.Transparency = transparency
						end
					end
				end
			end
		end
	end
end

task.wait(2)

function Switch6()
	for i,v in pairs(aparts) do
		v.CanCollide = false
		v.Transparency = transparency
		for i,v in pairs(bparts) do
			v.CanCollide = false
			v.Transparency = transparency
			for i,v in pairs(cparts) do
				v.CanCollide = false
				v.Transparency = transparency
				for i,v in pairs(dparts) do
					v.CanCollide = false
					v.Transparency = transparency
					for i,v in pairs(eparts) do
						v.CanCollide = false
						v.Transparency = transparency
						for i,v in pairs(fparts) do
							v.CanCollide = true
							v.Transparency = 0
						end
					end
				end
			end
		end
	end
end

task.wait(2)

function Switch7()
	for i,v in pairs(gparts) do
		v.CanCollide = true
		v.Transparency = 0
		for i,v in pairs(hparts) do
			v.CanCollide = false
			v.Transparency = transparency
			for i,v in pairs(iparts) do
				v.CanCollide = false
				v.Transparency = transparency
			end
		end
	end
end

task.wait(2)

function Switch8()
	for i,v in pairs(gparts) do
		v.CanCollide = false
		v.Transparency = transparency
		for i,v in pairs(hparts) do
			v.CanCollide = true
			v.Transparency = 0
			for i,v in pairs(iparts) do
				v.CanCollide = true
				v.Transparency = transparency
			end
		end
	end
end

task.wait(2)

function Switch9()
	for i,v in pairs(gparts) do
		v.CanCollide = false
		v.Transparency = transparency
		for i,v in pairs(hparts) do
			v.CanCollide = false
			v.Transparency = transparency
			for i,v in pairs(iparts) do
				v.CanCollide = true
				v.Transparency = 0
			end
		end
	end
end

task.wait(2)



function AttrQuai()
	if occupancy.VoieAOcc.Value == false and occupancy.VoieBOcc.Value == false and occupancy.VoieCOcc.Value == false and occupancy.VoieDOcc.Value == false and occupancy.VoieEOcc.Value == false and occupancy.VoieFOcc.Value == false then
		Switch1()
		
		print("Switch 1")
	else if	occupancy.VoieAOcc.Value == true and occupancy.VoieBOcc.Value == false and occupancy.VoieCOcc.Value == false and occupancy.VoieDOcc.Value == false and occupancy.VoieEOcc.Value == false and occupancy.VoieFOcc.Value == false then
			Switch2()
			Switch9()
			print("Switch 2")
	else if occupancy.VoieAOcc.Value == true and occupancy.VoieBOcc.Value == true and occupancy.VoieCOcc.Value == false and occupancy.VoieDOcc.Value == false and occupancy.VoieEOcc.Value == false and occupancy.VoieFOcc.Value == false then	
				Switch3()
				Switch7()
			print("Switch 3")
	else if occupancy.VoieAOcc.Value == true and occupancy.VoieBOcc.Value == true and occupancy.VoieCOcc.Value == true and occupancy.VoieDOcc.Value == false and occupancy.VoieEOcc.Value == false and occupancy.VoieFOcc.Value == false then
					Switch4()
					Switch9()
	else if occupancy.VoieAOcc.Value == true and occupancy.VoieBOcc.Value == true and occupancy.VoieCOcc.Value == true and occupancy.VoieDOcc.Value == true and occupancy.VoieEOcc.Value == false and occupancy.VoieFOcc.Value == false then				
						Switch5()
						Switch7()
	else if occupancy.VoieAOcc.Value == true and occupancy.VoieBOcc.Value == true and occupancy.VoieCOcc.Value == true and occupancy.VoieDOcc.Value == true and occupancy.VoieEOcc.Value == true and occupancy.VoieFOcc.Value == false then
							Switch9()
							task.wait(2)
							Switch6()
						else
							print("Attr échoué")
						end
					end					
				end			
			end	
		end
	end
end

task.wait(2)
print("Aiguille script a fini d'etre initialisee")
s1.Touched:connect(AttrQuai)
s2.Touched:connect(Switch1)
s3.Touched:connect(Switch2)
s4.Touched:connect(Switch3)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
image

Couldnt you just use GetDescendants and run checks?

the AParts and so on models are already ungrouped and do not need getdecendants

Would your functions work like this? The way you are doing it would run it hundreds of times.

–//

General functions
function Switch1()
for i,v in pairs(aparts) do
		v.CanCollide = true
		v.Transparency = 0
end
for i,v in pairs(bparts) do
		v.CanCollide = false
		v.Transparency = transparency
end
for i,v in pairs(cparts) do
	    v.CanCollide = false
		v.Transparency = transparency
end
for i,v in pairs(dparts) do
		v.CanCollide = false
		v.Transparency = transparency
end
for i,v in pairs(eparts) do
		v.CanCollide = false
		v.Transparency = transparency
end
for i,v in pairs(fparts) do
		v.CanCollide = false
		v.Transparency = transparency
end
3 Likes

i’m not surprised it’s causing lag spikes, i mean you got a whole nest of loops there

1 Like

Can you show the parts like what is it for ?

To fix that you can create union of the part to change their transparency together i think.

Or mesh, they have full triangle control.
So, they can be more performant.

@Dlbsp1983 has got your issue. Your for loops are nested inside each other, meaning that, as it went on, the for loops towards the end would run probably thousands of times, because of the previous for loops

Otherwise, you should not run into performance issues by changing the properties of 50 parts

for ...(script.Parent:GetDescendants()) do
  if v:IsA("Part") then
  ...
  end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.