Revise code to prevent memory leak

So basically I have this code in my racing game, but for some reason there is a memory leak inside of my game, and I have no idea if this is the script that is causing it. But I am pretty sure it is.

This script works, but once again may have a memory leak.

Please tell me how I can make this script better and more efficient, even if you do not see a memory leak coming from this.

local done = game.Workspace.Done -- Done and racing are bool values
local racing = game.Workspace.Racing
local chosenTrack = nil 
while true do
	Map = nil
		wait(10)
		Map = game.ServerStorage.Tracks.TrackOne:Clone()
		Map.Parent = game.Workspace
		done.Value = false
		wait(45)
		game.Workspace.TrackOne.BLOCKER.SurfaceGui.SIGN.Text = "3"
		wait(1)
		game.Workspace.TrackOne.BLOCKER.SurfaceGui.SIGN.Text = "2"
		wait(1)
		game.Workspace.TrackOne.BLOCKER.SurfaceGui.SIGN.Text = "1"
		wait(1)
		game.Workspace.TrackOne.BLOCKER.SurfaceGui.SIGN.Text = "GO!"
		game.Workspace.TrackOne.BLOCKER.SurfaceGui.SIGN.TextColor3 = Color3.fromRGB(0, 255, 0)
		game.Workspace.TrackOne.BLOCKER.Transparency = 1
		game.Workspace.TrackOne.BLOCKER.CanCollide = false
		wait(10)
		game.Workspace.TrackOne.BLOCKER.SurfaceGui.SIGN.TextTransparency = 1

if not done.Value == true then done.Changed:Wait()			
wait(10)
	Map:Destroy()
	Map = nil
	wait(10)
	Map = game.ServerStorage.Tracks.TrackTwo:Clone()
	Map.Parent = game.Workspace
	done.Value = false
	
	wait(45)
	game.Workspace.TrackTwo.BLOCKER.SurfaceGui.SIGN.Text = "3"
	wait(1)
	game.Workspace.TrackTwo.BLOCKER.SurfaceGui.SIGN.Text = "2"
	wait(1)
	game.Workspace.TrackTwo.BLOCKER.SurfaceGui.SIGN.Text = "1"
	wait(1)
	game.Workspace.TrackTwo.BLOCKER.SurfaceGui.SIGN.Text = "GO!"
	game.Workspace.TrackTwo.BLOCKER.SurfaceGui.SIGN.TextColor3 = Color3.fromRGB(0, 255, 0)
	game.Workspace.TrackTwo.BLOCKER.Transparency = 1
	game.Workspace.TrackTwo.BLOCKER.CanCollide = false
	wait(10)
	game.Workspace.TrackTwo.BLOCKER.SurfaceGui.SIGN.TextTransparency = 1
end
	if not done.Value == true then done.Changed:Wait()			
		wait(10)
		Map:Destroy()
		Map = nil
		wait(10)
		Map = game.ServerStorage.Tracks.TrackThree:Clone()
		Map.Parent = game.Workspace
		done.Value = false

		wait(45)
		game.Workspace.TrackThree.BLOCKER.SurfaceGui.SIGN.Text = "3"
		wait(1)
		game.Workspace.TrackThree.BLOCKER.SurfaceGui.SIGN.Text = "2"
		wait(1)
		game.Workspace.TrackThree.BLOCKER.SurfaceGui.SIGN.Text = "1"
		wait(1)
		game.Workspace.TrackThree.BLOCKER.SurfaceGui.SIGN.Text = "GO!"
		game.Workspace.TrackThree.BLOCKER.SurfaceGui.SIGN.TextColor3 = Color3.fromRGB(0, 255, 0)
		game.Workspace.TrackThree.BLOCKER.Transparency = 1
		game.Workspace.TrackThree.BLOCKER.CanCollide = false
		wait(10)
		game.Workspace.TrackThree.BLOCKER.SurfaceGui.SIGN.TextTransparency = 1
end
	if not done.Value == true then done.Changed:Wait()
		wait(10)
		Map:Destroy()
		Map = nil
	end
	end

A lot of your code can be cut down by using a simple for loop
I see that you have 3 tracks you are loading in, and to do that you are copying and pasting the code.
That is a solution, but it is not the fastest nor the most efficient.

I’ll give you a quick example of how i may solve the issue using a similar scenario.

local maps = maps:GetChildren()

-- this will loop through all the maps, do the code between do and end
-- then move onto the next map (pretty neat)
for _, map in pairs(maps) do
    print("3")
    wait(1)
    print("2")
    wait(1)
    print("1")
    wait(1)
    print("Go")
    -- I don't necessarily approve of the usage of wait() but you're a beginner so i'll let it slide.

    local clone = map:Clone()
    clone.Parent = workspace -- moving the map into the workspace
    wait(10)
    clone:Destroy()
end

This is a simple example of how for loops may work (I didn’t do it justice)
You may learn more about for loops here (they’ve very handy!)

Another issue i’m seeing in your code is that you don’t use variables that much.
Variables use up some memory but prevent repetition and can save time (And also optimize your code by preventing index calls, but lets not get ahead of ourselves)

Friendly Tip:
When you do Map:Destroy() you do not need to do Map = nil because :Destroy() does that all for you!

1 Like

Thanks for the tip. I’m a pretty messy coder, so I’ll revise even more.