I got a terribly long and terribly scripted line of code. It's really slow

Okay so, I know its bad, and unorganized, but I really want to know how I can speed the script up so its not lagging the server so much. I tried replacing ‘if’ on some of them with ‘elseif’ but that only broke the script. All I really need to know is how I can make the planets speed up a lot, how I can make sure its fast in the future, etc. It’s a big clump of code. A lot of things are put out, so I can keep them for later use since I was remaking the code. Surely there is a way to make less lag, or make the laggy moment at the start of the game shorter? Right now, after all the planets load there is no lag.

    colors = {"Slate", "Concrete", "Pebble", "Granite", "Sand", "Marble","Cobblestone"}
--types = {"Slate", "Concrete", "Pebble", "Granite", "Sand", "Marble"}
letter = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
--relfect = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1}
trans = {0.77,0.75,0.95,0.9,0.85,0.8,0.83,0.87,0.93,0.97}

Planets = 0
MaxPlanets = 60
Asteroids = 0
MaxAsteroids = 200
Folder =  Instance.new("Folder")
Folder.Parent = workspace
Folder.Name = "PlanetFolder"
repeat
	typed = math.random(1,3)
	if Planets < MaxPlanets then
	if typed == 1 then
	l = Instance.new("Part")
	l.Name = "Planet"
	l.Parent = Folder
	l.Orientation = Vector3.new(math.random(-360,360),math.random(-360,360),math.random(-360,360))
	--l.Shape = "Ball"
	l.Color = Color3.fromRGB(math.random(1, 120),math.random(1, 120),math.random(1, 120))
	size1 = math.random(40, 2048)
    size2 = math.random(40, 2048)
	size3 = math.random(40, 2048)
	l.Size = Vector3.new(size1, size2, size3)
	l.Position = Vector3.new(math.random(-150000, 150000),math.random(-50000, 100000),math.random(-150000, 150000))
	l.Material = (colors[math.random(1, #colors)])
	l.Anchored = true
		l.CanCollide = true
		Name = script.Parent.Namee:Clone()
		Name.Parent = l
		Name.Position = l.Position
		TextNum = math.random(1000, 9999)
		Letter = (letter[math.random(1, #letter)])
		Letter2 = (letter[math.random(1, #letter)])
		Name.BillboardGui.TextLabel.Text = Letter .. Letter2 .."-".. TextNum 
		timee = 0
	repeat 
	l2 = Instance.new("Part")
			l2.Parent = l
			l2.Color = l.Color
			if  math.random(1,8) == 1 then
		    l2.Color = Color3.fromRGB(math.random(-1,120),math.random(1,120),math.random(1,120))
	
		end

		l2.Material = l.Material
		 --l2.Reflectance = (relfect[math.random(1, #relfect)])
		l2.Size = l.size 
				l2.Orientation = Vector3.new(math.random(1,120),math.random(1,120),math.random(1,120))
				x =  l.Position.X
				y =  l.Position.y
				z =  l.Position.z
				sx = l.Size.X
				sy = l.Size.Y
				sz = l.Size.Z
				sox = math.random(-sx, sx)
				syx = math.random(-sy, sy)
				szx = math.random(-sz, sz)
				if sox >= 0 then
					  rx = x + sox
					end
				if sox < 0 then
					  rx = x - sox
					end
			    if syx >= 0 then
					  ry = y + syx
					end
				if syx < 0 then
					 ry = y - syx
					end
				if szx >= 0 then
					  rz = z + szx
					end
				if szx < 0 then
					  rz = z - szx
					end
	l2.Position = Vector3.new(rx, ry, rz)
	l2.Anchored = true
		l2.CanCollide = true
		timee = timee + 1
			until timee == 175
		--[[
	if size1 > 700 or size3 > 700 or size2 > 700 then
     	if math.random(1,21) == 18 then
		Ring = script.Parent.Ring:Clone()
		Ring.Parent = l
					Ring.Position = l.Position
				Ring.Transparency = 0.25
	    Ring.Orientation = Vector3.new(math.random(-360,360),math.random(-360,360),math.random(-360,360))
			 Ring.BrickColor = BrickColor.new(math.random(1, 255))
			end
	end
if size1 > 1400 or size3 > 1400 or size2 > 1400 then
	     if math.random(1,9) == 8 then
		Sphere = script.Parent.Sphere:Clone()
		Sphere.Parent = l
		Sphere.Sphere.Position = l.Position
		Sphere.Sphere2.Position = l.Position
				Sphere.Sphere.Transparency = (trans[math.random(1, #trans)])
				Sphere.Sphere2.Transparency = (trans[math.random(1, #trans)])
			Color = math.random (1,20)
			if Color == 3 then
					   Sphere.Sphere.Mesh.VertexColor = Vector3.new(1.5,1,1)
					Sphere.Sphere2.Sphere.Mesh.VertexColor = Vector3.new(1.5,1,1)
				end
			 if Color == 13 then
				 Sphere.Sphere.Mesh.VertexColor = Vector3.new(1,1.5,1)
					Sphere.Sphere2.Sphere.Mesh.VertexColor = Vector3.new(1,1.5,1)
				end
				 if Color == 20 then
					Sphere.Sphere.Mesh.VertexColor = Vector3.new(1,1,1.5)
	end
	end
 
			end
			  --]]
    Planets +=1
	end
	if typed == 2 or 3 then

	l = Instance.new("Part")
	l.Name = "Planet"
	l.Parent = Folder
	l.Orientation = Vector3.new(math.random(-360,360),math.random(-360,360),math.random(-360,360))
	--l.Shape = "Ball"
	l.Color = Color3.fromRGB(math.random(90, 115),math.random(90, 115),math.random(90, 115))
	size1 = math.random(40, 2048)
    size2 = math.random(40, 2048)
	size3 = math.random(40, 2048)
	l.Size = Vector3.new(size1, size2, size3)
	l.Position = Vector3.new(math.random(-150000, 150000),math.random(-50000, 100000),math.random(-150000, 150000))
	l.Material = (colors[math.random(1, #colors)])
	l.Anchored = true
		l.CanCollide = true
			Colour1 = math.random(1,170)
			Colour2 = math.random(1,170)
		Colour3 = math.random(1,170)
				Name = script.Parent.Namee:Clone()
		Name.Parent = l
		Name.Position = l.Position
		TextNum = math.random(1000, 9999)
		Letter = (letter[math.random(1, #letter)])
		Letter2 = (letter[math.random(1, #letter)])
		Name.BillboardGui.TextLabel.Text = Letter .. Letter2 .."-".. TextNum 
timee = 0
	repeat 
	l2 = Instance.new("Part")
			l2.Parent = l

			--l2.Color = Color3.fromRGB(math.random(1,170),math.random(1,170),math.random(1,170))
			l2.Color = Color3.fromRGB(Colour1, Colour2, Colour3)
			colour = math.random(1,5)
			if colour == 1 or colour == 2 or colour == 3 or colour == 4 then
				l2.Color = l.Color
	
		end

		l2.Material = l.Material
		 --l2.Reflectance = (relfect[math.random(1, #relfect)])
		l2.Size = l.size
				l2.Orientation = Vector3.new(math.random(1,170),math.random(1,170),math.random(1,170))
				x =  l.Position.X
				y =  l.Position.y
				z =  l.Position.z
				sx = l.Size.X
				sy = l.Size.Y
				sz = l.Size.Z
				sox = math.random(-sx, sx)
				syx = math.random(-sy, sy)
				szx = math.random(-sz, sz)
				if sox >= 0 then
					  rx = x + sox
					end
				if sox < 0 then
					  rx = x - sox
					end
			    if syx >= 0 then
					  ry = y + syx
					end
				if syx < 0 then
					 ry = y - syx
					end
				if szx >= 0 then
					  rz = z + szx
					end
				if szx < 0 then
					  rz = z - szx
					end
	l2.Position = Vector3.new(rx, ry, rz)
	l2.Anchored = true
		l2.CanCollide = true
		timee = timee + 1
			until timee == 150
			--[[
	if size1 > 700 or size3 > 700 or size2 > 700 then
     	if math.random(1,21) == 18 then
		Ring = script.Parent.Ring:Clone()
		Ring.Parent = l
		Ring.Position = l.Position
				Ring.Transparency = 0.25
	    Ring.Orientation = Vector3.new(math.random(-360,360),math.random(-360,360),math.random(-360,360))
			 Ring.BrickColor = BrickColor.new(math.random(1, 255))
			end
	end
	if size1 > 1400 or size3 > 1400 or size2 > 1400 then
	     if math.random(1,9) == 8 then
		Sphere = script.Parent.Sphere:Clone()
				Sphere.Parent = l	
				Sphere.Sphere.Position = l.Position
		Sphere.Sphere2.Position = l.Position
				Sphere.Sphere.Transparency = (trans[math.random(1, #trans)])
				Sphere.Sphere2.Transparency = (trans[math.random(1, #trans)])
			Color = math.random (1,20)
			if Color == 3 then
					  Sphere.Sphere.Mesh.VertexColor = Vector3.new(1.5,1,1)
					  Sphere.Sphere2.Mesh.VertexColor = Vector3.new(1.5,1,1)
				end
			 if Color == 13 then
				 Sphere.Sphere.Mesh.VertexColor = Vector3.new(1,1.5,1)
			    Sphere.Sphere2.Mesh.VertexColor = Vector3.new(1,1.5,1)
				end
				 if Color == 20 then
					Sphere.Sphere.Mesh.VertexColor = Vector3.new(1,1,1.5)
					Sphere.Sphere2.Mesh.VertexColor = Vector3.new(1,1,1.5)
	end
	end

			end
			--]]
    Planets +=1
      
		end
	end
	if Asteroids < MaxAsteroids then
	if  typed == 4 or typed == 5 or typed == 6 then
			
	l = Instance.new("Part")
	l.Name = "Rock"
	l.Parent = Folder
	l.Orientation = Vector3.new(math.random(-360,360),math.random(-360,360),math.random(-360,360))
	--l.Shape = "Ball"
	l.BrickColor = BrickColor.new(math.random(1, 4))
	size1 = math.random(20, 100)
    size2 = math.random(20, 100)
	size3 = math.random(20, 100)
	l.Size = Vector3.new(size1, size2, size3)
	l.Position = Vector3.new(math.random(-150000, 150000),math.random(-50000, 100000),math.random(-150000, 150000))
	l.Material = (colors[math.random(1, #colors)])
	l.Anchored = true
	l.CanCollide = true
timee = 0
	repeat 
	l2 = Instance.new("Part")
		l2.Parent = l
			l2.BrickColor = l.BrickColor
		l2.Material = l.Material
		 --l2.Reflectance = (relfect[math.random(1, #relfect)])
		l2.Size = l.size 
	l2.Orientation = Vector3.new(math.random(-360,360),math.random(-360,360),math.random(-360,360))
	l2.Position = l.Position --+ --Vector3.new(math.random(1, 4),math.random(1, 4),math.random(1, 4))
	l2.Anchored = true
		l2.CanCollide = true
		timee = timee + 1
	until timee == 20
    Asteroids +=1
		end
	end
	wait()
until Planets >= MaxPlanets and Asteroids >= MaxAsteroids
script.Disabled = true

--Made by Korbork!

Sorry if a bit of a bad topic or if it seems im asking for the wrong things, I’m new at this.

1 Like

Try using local variables instead of global ones.

3 Likes

is there any reason this needs to be on the server and not on the client?
if switching to client won’t adversely affect the game’s functionality, then going client-side should be a considerable improvement in this case.

2 Likes

Well it generates the entire map for every single player, making a bunch of planets. I don’t think it’s possible for it to be on the client.

1 Like

I agree with what @Qxual said but also try setting all the properties before parenting the part, check out this post to see my reasoning:

1 Like

You need to split sections of code out into separate functions and then give those functions sensible names. Right now your code is really hard to read, because it’s just one long wall of text. E.g. if there’s a part of the code that’s responsible for generating a new planet, make a function called “generatePlanet” and call that function instead.

4 Likes

Thanks everyone! It helped a lot! I wish I could give solution to multiple people!