Finally found my memory leak, don't know how to patch it

I previously made a post asking why my game was getting insanely laggy. I found out that it was a memory leak and I spent half an hour locating it. The problem? I can’t figure out how to patch it, and have spent around an hour researching and looking through this code trying to find what isn’t being collected.

events.Remote.Run.OnServerEvent:Connect(function(plr, held)
	local char:Model = plr.Character
	local anims = char:FindFirstChild("Anims")
	--print(char:GetAttribute("Stamina"))
	
	if plr.InRound.Value ~= true then
		return
	end
	
	if held == true and char:GetAttribute("Stamina") > 0 and plr.Character.HumanoidRootPart.AssemblyLinearVelocity.Magnitude > 0 and plr.InRound.Value == true then
		--char:SetAttribute("Speed", char:GetAttribute("RunSpeed") * char.SpeedMultiplier.Value)
		if char:GetAttribute("Stamina") >= 10 then
			char:SetAttribute("Stamina", char:GetAttribute("Stamina") - 0.3)
			
			plr.PlayerGui.GameGui.Stats.StaminaBar.Bar.BackgroundColor3 = Color3.new(1, 1, 1)

			if char:GetAttribute("Running") == false then
				char:SetAttribute("Running", true)
				--char:SetAttribute("Speed", char:GetAttribute("RunSpeed") * char.SpeedMultiplier.Value)

				char.Humanoid:LoadAnimation(char.Anims.Run):Play(0.2, 1, char.SpeedMultiplier.Value)
			end

			char:SetAttribute("Speed", char:GetAttribute("RunSpeed") * char.SpeedMultiplier.Value)

			for i, track:AnimationTrack in char.Humanoid:GetPlayingAnimationTracks() do
				if track.Name == "Run" then
					track:AdjustSpeed(char.SpeedMultiplier.Value)
				end
			end

			--task.wait(0.01)

			repeat task.wait() until not held
		else
			char:SetAttribute("Stamina", char:GetAttribute("Stamina") - 0.3)
			plr.PlayerGui.GameGui.Stats.StaminaBar.Bar.BackgroundColor3 = Color3.new(1, 0, 0)
		end
		--[[char:SetAttribute("Stamina", char:GetAttribute("Stamina") - 0.3)
		
		if char:GetAttribute("Running") == false then
			char:SetAttribute("Running", true)
			--char:SetAttribute("Speed", char:GetAttribute("RunSpeed") * char.SpeedMultiplier.Value)
			
			char.Humanoid:LoadAnimation(char.Anims.Run):Play(0.2, 1, char.SpeedMultiplier.Value)
		end
		
		char:SetAttribute("Speed", char:GetAttribute("RunSpeed") * char.SpeedMultiplier.Value)
		
		for i, track:AnimationTrack in char.Humanoid:GetPlayingAnimationTracks() do
			if track.Name == "Run" then
				track:AdjustSpeed(char.SpeedMultiplier.Value)
			end
		end
		
		--task.wait(0.01)
		
		repeat task.wait() until not held]]
	end
	if held == false or char:GetAttribute("Stamina") <= 0 or plr.Character.HumanoidRootPart.AssemblyLinearVelocity.Magnitude == 0 and state == "Round"  and plr.InRound.Value == true  then
		plr.PlayerGui.GameGui.Stats.StaminaBar.Bar.BackgroundColor3 = Color3.new(1, 1, 1)
		
		char:SetAttribute("Speed", char:GetAttribute("WalkSpeed") * char.SpeedMultiplier.Value)
		char:SetAttribute("Running", false)
		if char:GetAttribute("Stamina") <= char:GetAttribute("MaxStamina") and held == false then
			char:SetAttribute("Stamina", char:GetAttribute("Stamina") + 0.35)
			if char:GetAttribute("Stamina") > char:GetAttribute("MaxStamina") then
				char:SetAttribute("Stamina", char:GetAttribute("MaxStamina"))
			end
			--task.wait(0.01)
		end
		
		--[[for i, track:AnimationTrack in char.Humanoid:GetPlayingAnimationTracks() do
			if track.Name == "Run" then
				track:Stop(0.4)
			end
		end]]
	end
	
	held = nil
	char = nil
	anims = nil
end)

Keep in mind that I am VERY new to memory leaks and how to spot them. I just need help finding out where it is, I can’t find it no matter how much I research or read through the code.

1 Like

how often is the “run” event fired

Constantly, I would not be surprised if this is the problem. My system is honestly awful and is still in dire need of an overhaul.

The only thing that looks suspicious is the constant LoadAnimation calls in my opinion (for running), other than that I don’t see where a memory leak could appear, honestly, there’s just not enough info or anything sadly

well you’re doing a lot of :loadaniamtion on your characters. Also setting attributes a lot but that sohuldn’t be the rpoblem.

Yeah, that’s what really messing with me. It feels like nothing (besides the constant event firing) could cause the leak, but using the dev console says otherwise.

Tried getting rid of them to see, still generating a bunch of lag.

1 Like

the thing is that the only real thing that could be is the LoadAnimation, honestly. Other than that you could be getting misled perhaps? Maybe some other code inside that script could be causing the issue, however I cannot assert that since we only have the event code body

1 Like

ok so what if you instead of fireing the remoteevnts SIMULATE it in a localscript as if it’d being fired so like

renderstep do

your code

end

then you can see if it’s because of the remote firing or just because of the code

I does appear to be the code. All the lag is now generated on the client.

Try removing the attribute setting And see if that makes it lag anymore.

Still generates lag, I genuinely can’t figure out what it could be.

yeah idk why it’s doing that can you show the server’s remote code? (even tho it may not help)

local elapsed = 0
game["Run Service"].Heartbeat:Connect(function(deltaTime)
	elapsed += deltaTime
	if elapsed >= 1/60 then
		elapsed = 0;
		events.Remote.Run:FireServer(held)
	end
end)

should probably only load these once and then just play them back, this creates a AnimationTrack instance every time it runs and plays it

The thing is, even if the load animation isn’t present, the same amount of lag is.

The lag is caused by you firing the event every heatbeat! That entire block of code is being run by the server every heartbeat for every single Client. One client would cause lag multiple would just crash the server.

You need to change your script to be event based rather than loop based. Make it so when x happens then the code will run rather than checks every hearbeat. Lua is a event based programming language so there is no way it can handle this amount of code every single heartbeat.

4 Likes

You’re updating client UI from the server. Maybe try handling it only on the client?

Yeah, I was planning to overhaul the running code entirely since yesterday so I could kill 2 birds with one stone. Fix my memory leak and fix my awful system.

1 Like

Sounds good, let me know if you’re struggling with implementing it.

1 Like