Anti-cheat script causing memory leaks

So, this is my anti-cheat script. Not much explaining to do really, but thanks for any feedback!

	player.CharacterAdded:Connect(function(char)
		wait(3)
		local firstCF = char:WaitForChild("HumanoidRootPart").CFrame
		local lastY = char.HumanoidRootPart.Position.Y
		local antiCheats = 0

		local stillInAir = false
		local falling = false
		
		local lastCF

		local humanoid = char:WaitForChild("Humanoid")
		humanoid.FreeFalling:Connect(function()
			falling = true
		end)

		spawn(function()
			while wait(0.8) do
				if game.ReplicatedStorage.Values.Game.Value == true then
					if char:FindFirstChild("HumanoidRootPart") then
						lastCF = char.HumanoidRootPart.CFrame
						if lastCF then
							if (lastCF.Position - char.HumanoidRootPart.Position).Magnitude >= 100 then
								if not player:FindFirstChild("Tele") then
									char.HumanoidRootPart.CFrame = lastCF
								end
							end
						end
						if (lastY-char.HumanoidRootPart.Position.Y) <= -25 then
							if not player:FindFirstChild("Tele") then
								char.HumanoidRootPart.CFrame = lastCF
							end
						end
						if humanoid.FloorMaterial == Enum.Material.Air then
							if stillInAir == false then
								if falling == false then
									stillInAir = true
								end
							else
								if falling == false then
									antiCheats += 1
									if antiCheats == 5 then
										antiCheats = 0
										char.HumanoidRootPart.CFrame = firstCF
									else
										char.HumanoidRootPart.CFrame = lastCF
									end
									stillInAir = false
								end
							end
						else
							falling = false
						end
					end
					if char:FindFirstChild("HumanoidRootPart") then
						lastY = char.HumanoidRootPart.Position.Y
						lastCF = char.HumanoidRootPart.CFrame
					end
				end
			end
		end)
		spawn(function()
			while wait(120) do
				antiCheats = 0
			end
		end)
	end)
end)

You’re running while true do loops even after the player has died. Add a Humanoid.Died function to break the while true do loops so they don’t repeat even after death.

1 Like

thx, will do. Solutioning in a bit there’s a storm coming and I can’t go on my computer rn ._.

1 Like

alright, I’m back on laptop. Here is what I did, and I wanted to see what you thought:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		wait(3)
		local firstCF = char:WaitForChild("HumanoidRootPart").CFrame
		local lastY = char.HumanoidRootPart.Position.Y
		local antiCheats = 0

		local stillInAir = false
		local falling = false
		
		local lastCF
		
		local died = false

		local humanoid = char:WaitForChild("Humanoid")
		local conn = humanoid.FreeFalling:Connect(function()
			falling = true
		end)
		
		local conn1 = humanoid.Died:Connect(function()
			died = true
		end)

		spawn(function()
			while wait(0.8) do
				if died == true then conn:Disconnect() conn1:Disconnect() break end
				if game.ReplicatedStorage.Values.Game.Value == true then
					if char:FindFirstChild("HumanoidRootPart") then
						lastCF = char.HumanoidRootPart.CFrame
						if lastCF then
							if (lastCF.Position - char.HumanoidRootPart.Position).Magnitude >= 100 then
								if not player:FindFirstChild("Tele") then
									char.HumanoidRootPart.CFrame = lastCF
								end
							end
						end
						if (lastY-char.HumanoidRootPart.Position.Y) <= -25 then
							if not player:FindFirstChild("Tele") then
								char.HumanoidRootPart.CFrame = lastCF
							end
						end
						if humanoid.FloorMaterial == Enum.Material.Air then
							if stillInAir == false then
								if falling == false then
									stillInAir = true
								end
							else
								if falling == false then
									antiCheats += 1
									if antiCheats == 5 then
										antiCheats = 0
										char.HumanoidRootPart.CFrame = firstCF
									else
										char.HumanoidRootPart.CFrame = lastCF
									end
									stillInAir = false
								end
							end
						else
							falling = false
						end
					end
					if char:FindFirstChild("HumanoidRootPart") then
						lastY = char.HumanoidRootPart.Position.Y
						lastCF = char.HumanoidRootPart.CFrame
					end
				end
			end
		end)
		spawn(function()
			while wait(120) do
				antiCheats = 0
				if died == true then break end
			end
		end)
	end)
end)
1 Like