Massive Memory Leak in script

Hi!
The title is self explanitory.
This script is adding 100mb of memory every 100 frames.
What’s going on?
Thanks!

-- Written by p0s_0, Modified by DarkPixz

local plr = game.Players.LocalPlayer
local runService = game:GetService("RunService")
local userInput = game:GetService("UserInputService")
local mouse = plr:GetMouse()
local mouseFr = script.Parent

local printDebug = false 

-- Functions --

function toggleNewMouse(enabled)
	if enabled then
		mouseFr.Visible = false
		mouse.Icon = "rbxasset://Cursors/KeyboardMouse/ArrowFarCursor.png"
		userInput.MouseIconEnabled = true
	else
		mouseFr.Visible = true
		mouse.Icon = ""
		userInput.MouseIconEnabled = false
	end
end

function updateMouse(x, y)
	mouseFr.Position = UDim2.new(0, x, 0, y) 
	for v,i in pairs(plr.PlayerGui:GetDescendants()) do
		if i:IsA("GuiObject") then 
			i.MouseEnter:Connect(function()
				if i.BackgroundTransparency ~= 1 then 
					mouseFr.Image = "http://www.roblox.com/asset/?id=7027845989"
				end
			end)

			i.MouseLeave:Connect(function()
				mouseFr.Image = "http://www.roblox.com/asset/?id=7027810711"
			end)
		end
	end
	if printDebug then
		print("Updated mouseFr position to " .. tostring(x) .. ", " .. tostring(y))
	end
end

-- Main Logic --
toggleNewMouse(false)

while wait() do
	updateMouse(mouse.X - 32, mouse.Y + 4) -- we negate 32 from X and add 4 to Y so the position for mouseFr is accurate
end

You connecting 2 events a lot of times, maybe use a event instead.

-- Written by p0s_0, Modified by DarkPixz

local plr = game.Players.LocalPlayer
local runService = game:GetService("RunService")
local userInput = game:GetService("UserInputService")
local mouse = plr:GetMouse()
local mouseFr = script.Parent

local printDebug = false 

-- Functions --

function toggleNewMouse(enabled)
	if enabled then
		mouseFr.Visible = false
		mouse.Icon = "rbxasset://Cursors/KeyboardMouse/ArrowFarCursor.png"
		userInput.MouseIconEnabled = true
	else
		mouseFr.Visible = true
		mouse.Icon = ""
		userInput.MouseIconEnabled = false
	end
end

function updateMouse(x, y)
	mouseFr.Position = UDim2.new(0, x, 0, y) 

	if printDebug then
		print("Updated mouseFr position to " .. tostring(x) .. ", " .. tostring(y))
	end
end

function connectEvents(guiObject)
    if guiObject:IsA("GuiObject") then 
		guiObject.MouseEnter:Connect(function()
		    if guiObject.BackgroundTransparency ~= 1 then 
			    mouseFr.Image = "http://www.roblox.com/asset/?id=7027845989"
			end
		end)

		guiObject.MouseLeave:Connect(function()
			mouseFr.Image = "http://www.roblox.com/asset/?id=7027810711"
		end)
	end
end

-- Main Logic --

toggleNewMouse(false)
plr.PlayerGui.DescendantAdded:Connect(connectEvents)

for _, descendant in pairs(plr.PlayerGui:GetDescendants()) do
    connectEvents(descendant)
end

while wait() do
	updateMouse(mouse.X - 32, mouse.Y + 4) -- we negate 32 from X and add 4 to Y so the position for mouseFr is accurate
end
1 Like

It fixed the memory leak, but the cursor isn’t smooth.

It also seems to only correctly change the cursor half of the time.