Can I make this code any more optimized?

So this code just generates a road and places it in front of the next. A bit like doors room generation. Now I was just wondering if there is any way I could make it more optimized or if there is a memory leak from it due to the connections being made. Thanks :slight_smile:

Code:

local ServerS = game:GetService('ServerStorage')

local Roads = ServerS:WaitForChild('Roads'):GetChildren()

local MadeRoads = {}

local LastNumber

local LastRoad

local Handler = {}

Handler.CreateRoad = function()

	local LastPosition

	local PickedNumber = math.random(1,#Roads)

	if LastNumber ~= nil then
		if PickedNumber == LastNumber then
			repeat
				PickedNumber = math.random(1,#Roads)
				task.wait(0.001)
			until PickedNumber ~= LastNumber
		end
	end

	local NewRoad = Roads[PickedNumber]:Clone()

	LastNumber = PickedNumber

	if not LastRoad then
		LastPosition = Vector3.new(-NewRoad.PrimaryPart.Size.X/2,0,0)
	else
		LastPosition = LastRoad.PrimaryPart.Position
	end
	
	
	NewRoad:PivotTo(CFrame.new(LastPosition + Vector3.new(NewRoad.PrimaryPart.Size.X,0,0)))

	NewRoad.Parent = game.Workspace.Roads	

	print(NewRoad.Name)
	
	table.insert(MadeRoads,NewRoad)
	
	--task.spawn(function()
		
		Handler.ControlRoad(NewRoad)
		
	--end)
	
	LastRoad = NewRoad
	
	return NewRoad
end


Handler.ControlRoad = function(Road)
	

	local Debounce = false
	local Debounce1 = false
	
	
	
	
	Road.Start.Touched:Connect(function(hit)

		if not hit.Parent then
			return
		end

		if not hit.Parent:FindFirstChild("Humanoid") then
			return
		end


		if Debounce == true then
			return
		end

		Debounce = true

		if table.find(MadeRoads,Road) == 3 then

			MadeRoads[1]:Destroy()
			table.remove(MadeRoads,1)
		end

		return

	end)

	Road.End.Touched:Connect(function(hit)

		if not hit.Parent then
			return
		end

		if not hit.Parent:FindFirstChild("Humanoid") then
			return
		end

		if Debounce1 == true then
			return
		end

		Debounce1 = true

		LastRoad = Handler.CreateRoad()
		
		LastRoad = Handler.CreateRoad()
		
		

		return
	end)
	
	return
end


return Handler

It’s actually not bad at all. You will have memory leaks if you do continue to create any sort of connections and don’t handle them.

What you can do, is just set a connection so:

local connection

connection = TouchedConnection:Connect(etc etc)

end

Then use connection:Disconnect() and set connection = nil so lua’s garbage collector can take it.

Instead of using a loop here

I’d use recursion so:

Handler.GenerateNumber = function()
    	local PickedNumber = math.random(1,#Roads)
        if LastNumber ~= nil then
              if PickedNumber == LastNumber then
			return Handler.GenerateNumber()
              else
                   return PickedNumber
		end
	end
end 

Instead of a loop like that. If you don’t understand what recursion is, it’s literally just a function calling itself again. One of the issues with recursion is that if it calls itself too much, roblox will stop the script from running, as that process is extremely lag inducing, so make sure it ends.

I think you’re good though.

1 Like