Help with Memory Leak/ mouse using too much memory

I’m dealing with memory issues in the project I am currently working on, and after struggling through the developer console, it seems as though the most expensive local script I currently have is the following.

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local values = game.ReplicatedStorage:WaitForChild("values")
local tileSelection = values:WaitForChild(player.Name.." tileSelection")
local tileSelectedBool = values:WaitForChild(player.Name.." tileSelectedBool")
local teamAffiliation = values:WaitForChild(player.Name.." teamAffiliation")
local tileDrag = game.Players.LocalPlayer.PlayerScripts:WaitForChild("tileDrag")
local mouse = player:GetMouse()
local selection = game.ReplicatedStorage.selection
local highlightedTiles = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("highlightedTiles"))
local selectionCopy

mouse.Move:Connect(function()
	
	if mouse.Target then
		
		if mouse.Target.Name == "Hitbox" then
			
			local Hitbox = mouse.Target
			local tile = Hitbox.Parent
			local tileNum = tile:GetAttribute("tileNum")
			
			if tileNum ~= tileDrag.Value then
				
				tileDrag.Value = tileNum
				
				if selectionCopy then
					
					selectionCopy:Destroy()
					
				end
				
				if not tile:FindFirstChild("selection") then
					
					selectionCopy = selection:Clone()
					selectionCopy.Position = Vector3.new(tile.Position.X, 70.025, tile.Position.Z)
					
					if tile:GetAttribute("Occupied") ~= teamAffiliation.Value then
						
						if teamAffiliation.Value == "red" then
							
							if tile:GetAttribute("Occupied") == "blue" then
								
							selectionCopy.BrickColor = BrickColor.new("Really red")
								
							end
							
						elseif teamAffiliation.Value == "blue" then

							if tile:GetAttribute("Occupied") == "red" then

								selectionCopy.BrickColor = BrickColor.new("Really red")

							end

						end
						
						if tile:GetAttribute("Occupied") == "Water" or tile:GetAttribute("Occupied") == "" then
							
							selectionCopy.BrickColor = BrickColor.new("Medium stone grey")
							
						end
					
					end
					
					selectionCopy.Parent = tile
					
				end
			
			end

		else
			
			tileDrag.Value = 0
			
			if selectionCopy then
				
				selectionCopy:Destroy()
				
			end
		
		end
		
	end

end)

tileSelection.Changed:Connect(function()
	
	if tileSelection.Value == tileDrag.Value then
		
		if selectionCopy then
			
			selectionCopy:Destroy()
			
		end
			
			
	end
		
end)

Essentially it “highlights” the tile the player’s mouse is on, by creating a copy of an instance stored in ReplicatedStorage, and then changing its colour based on the tile status. I’m practically certain I need to disconnect this function at some point or optimize when it fires a signal, but I haven’t the slightest idea as to the approach I should take.

Because you are cloning a part and placing it into workspace every time the mouse moves and even though cloning is usually efficient compared to instance creation, it still isn’t that performant to clone a part every time the mouse moves. Rather create a single part and move it to the mouse location with the necessary color changes every time the mouse moves. Also Highlights exist, in case you didn’t know.

1 Like