Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to reduce exponential lag from script use?

This script here;

local RockScript = require(game.ServerScriptService.RockScript)
print(RockScript.allrocktable)

rock = script.Parent
local startpos = rock.Position
table.insert(RockScript.allrocktable, rock.Position)

no_right_rock = 0
no_left_rock = 0
no_back_rock = 0
no_front_rock = 0
no_up_rock = 0
no_down_rock = 0

local toolactive
 toolactive = game.ServerStorage.toolactive.Event:Connect(function(player, mousetarget)
	if rock.Transparency ~= 1 then
		print(player)
		print(mousetarget)
		print("rockgone")
		if mousetarget == rock then
			rock.Transparency = 1
		end
	end
	
	
end)



local propchange
 propchange = rock:GetPropertyChangedSignal("Transparency"):Connect(function()
	if rock.Transparency == 1 then
		rock.CanCollide = false
		local right_pos = Vector3.new(startpos.X-rock.Size.X,startpos.Y,startpos.Z)
		
		
		if table.find(RockScript.allrocktable, right_pos) then
			print("pos already used")
			no_right_rock = 1
			end
			
		
		if no_right_rock == 0 then
			local coal = math.random(1,2)
			local right_rock = rock:Clone()
			
			table.insert(RockScript.allrocktable, right_rock.Position)
			
			
			right_rock.CanCollide = true
			right_rock.Position = right_pos




			right_rock.Name = "rightrock"
			right_rock.Transparency = NumberSequence.new(0)
			right_rock.Parent = workspace.rocks
			print("done")
			print(RockScript.allrocktable)
			
		end
		no_right_rock = 0
		
		-------------------------------------------
		
		local left_pos = Vector3.new(startpos.X+rock.Size.X,startpos.Y,startpos.Z)

		if table.find(RockScript.allrocktable, left_pos) then
			no_left_rock = 1
			print("pos already used")
				end
		if no_left_rock == 0 then
			local left_rock = rock:Clone()
			table.insert(RockScript.allrocktable, left_rock.Position)
			left_rock.CanCollide = true
			left_rock.Position = left_pos




			left_rock.Name = "leftrock"
			left_rock.Transparency = NumberSequence.new(0)
			left_rock.Parent = workspace.rocks
			print("done")
			print(RockScript.allrocktable)
		end
		no_left_rock = 0
		
		-------------------------------------------
		
		
		local back_pos = Vector3.new(startpos.X,startpos.Y,startpos.Z+rock.Size.Z)
			
		if table.find(RockScript.allrocktable, back_pos) then
			no_back_rock = 1
			print("pos already used")
			end
			
		if no_back_rock == 0 then
			local back_rock = rock:Clone()
			table.insert(RockScript.allrocktable, back_rock.Position)
			back_rock.CanCollide = true
			back_rock.Position = back_pos




			back_rock.Name = "backrock"
			back_rock.Transparency = NumberSequence.new(0)
			back_rock.Parent = workspace.rocks
			print("done")
			print(RockScript.allrocktable)

		end
		no_back_rock = 0
		-------------------------------------------

		local front_pos = Vector3.new(startpos.X,startpos.Y,startpos.Z-rock.Size.Z)

		if table.find(RockScript.allrocktable, front_pos) then
			no_front_rock = 1
			print("pos already used")
			end

		if no_front_rock == 0 then
			local front_rock = rock:Clone()
			table.insert(RockScript.allrocktable, front_rock.Position)
			front_rock.CanCollide = true
			front_rock.Position = front_pos




			front_rock.Name = "frontrock"
			front_rock.Transparency = NumberSequence.new(0)
			front_rock.Parent = workspace.rocks
			print("done")
			print(RockScript.allrocktable)

		end
		no_front_rock = 0
		-------------------------------------------
		
		local up_pos = Vector3.new(startpos.X,startpos.Y+rock.Size.Y,startpos.Z)

		if table.find(RockScript.allrocktable, up_pos) then
			no_up_rock = 1
			print("pos already used")
			end

		if no_up_rock == 0 then
			local up_rock = rock:Clone()
			table.insert(RockScript.allrocktable, up_rock.Position)
			up_rock.CanCollide = true
			up_rock.Position = up_pos




			up_rock.Name = "uprock"
			up_rock.Transparency = NumberSequence.new(0)
			up_rock.Parent = workspace.rocks
			print("done")
			print(RockScript.allrocktable)
		end
			no_up_rock = 0


		-------------------------------------------
		
		local down_pos = Vector3.new(startpos.X,startpos.Y-rock.Size.Y,startpos.Z)

		if table.find(RockScript.allrocktable, down_pos) then
			no_down_rock = 1
			print("pos already used")
			end

		if no_down_rock == 0 then
			local down_rock = rock:Clone()
			table.insert(RockScript.allrocktable, down_rock.Position)
			down_rock.CanCollide = true
			down_rock.Position = down_pos




			down_rock.Name = "downrock"
			down_rock.Transparency = NumberSequence.new(0)
			down_rock.Parent = workspace.rocks
			print("done")
			print(RockScript.allrocktable)
		end
		no_down_rock = 0


		-------------------------------------------
		
		
		
		
		rock:Destroy()
		
		
	end
end)

rock:GetPropertyChangedSignal("Position"):Connect(function()
	startpos = rock.Position
end)



Works fine the first 6 or so runs and then it starts to lag, very, very badly, as shown in this video https://vimeo.com/806579252

Hopefully you can see it lags more and more every use, I have no idea what causes it, any help is appreciated.

1 answer

I feel a bit stupid, I really didn't think the prints were the issue, but after spending a while trying other things, i get rid of them, and surprise surprise, there's no lag. It also makes sense that was problem since printing the table would get harder and harder each time.

This discussion is preserved as it was. New answers, comments, and votes are closed.