Memory Leakage Help | Freeze Tag Script

:wave:t2: Hi Developers!

This is my first post using the Roblox Devforum, so If I’m doing anything wrong, please feel free to tell me! :wink:

I have a problem. I have discovered a huge memory leak in my freeze tag game where intense lag quickly grows and Client Memory Usage skyrockets. (As seen below)

I found the exact script where this problem occurs, however I have no idea how I would fix this memory leakage. Here’s my code:

--[[ --// INFORMATION //--
		WRITERS(S) = KingCh1ll
		CREATION DATE = 1/11/2022
		LAST EDITED DATE = 1/11/2022
		DETAILS = Freeze tag script with memory leakage. Hopefully to be fixed soon.
--]]

while wait(0.5) do
	local Hitbox = script.Parent:WaitForChild("Hitbox")

	Hitbox.Touched:Connect(function(hit)
		if (not hit.Parent or not game.Players:GetPlayerFromCharacter(hit.Parent)) then
			return
		end

		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if (Player) then
			local Self = game.Players:GetPlayerFromCharacter(script.Parent)
			local Humanoid = hit.Parent:WaitForChild("Humanoid")

			if (Self:FindFirstChild("Tagger")) then
				-- Check if the user is already frozen.
				
				if (hit.Parent:FindFirstChild("FreezeBlock")) then
					return
				end
				
				-- Freeze the user
				

				local FreezeBlock = game.ReplicatedStorage.Storage.FrozenBlock:Clone()
				
				if (hit.Parent:FindFirstChild("HumanoidRootPart")) then
					hit.Parent:FindFirstChild("HumanoidRootPart").Anchored = true
					
					FreezeBlock.Position = hit.Parent:FindFirstChild("HumanoidRootPart").Position
				end
				
				FreezeBlock.Parent = hit.Parent

				Humanoid.WalkSpeed = 0
			else
				-- Thaw the user.

				if (hit.Parent:FindFirstChild("HumanoidRootPart")) then
					hit.Parent:FindFirstChild("HumanoidRootPart").Anchored = false
				end

				if (hit.Parent:FindFirstChild("FreezeBlock")) then
					hit.Parent:FindFirstChild("FreezeBlock"):Destroy()
				end

				Humanoid.WalkSpeed = 16
			end
		end
	end)
end

Have you tried removing the while loop?

1 Like

Hi! I have no idea why I didn’t think of this in the first place. When I removed it, it worked without lag or memory problems. Thank you!

1 Like