Possable Memory Leak?

When swimming in my game, ping slowly climbs up to the high 7-10k. This doesnt happen when testing in studio only on he actual game whe multiple people are swimming.

The Code:

local cam = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local char = player.Character

local swimmingRightNow = false

local IsDamaging = false

local IsPlayingDrowningAnim = false

local LockYAxis = Instance.new("BodyPosition")

local SwimAnim = char.Humanoid:WaitForChild("Animator"):LoadAnimation(char.Info.Animations.Movement.Swim)
local DrownAnim = char.Humanoid:WaitForChild("Animator"):LoadAnimation(char.Info.Animations.Movement.Drown)


local function stopAllAnimations()

	for i,v in pairs(char:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
		if v.Name == "Idle" or v.Name == "Walk" or v.Name == "Run" or v.Name == "Fall" then
			v:Stop(char.Info.Animations.Movement.TransitionTimes.WalkTransition.Value)
		end
	end

end

local function DrownLoop()
	
if IsDamaging == false and swimmingRightNow == true then
		
		IsDamaging = true
		
		while task.wait(1) do
			
			print("DROWN---------------------------")
			
			if char:WaitForChild("Humanoid").Health <= 0 then
				break
			end
			
		if char.Info.Stats.Stamina.CurStamAmnt.Value > 0 or swimmingRightNow == false then
				IsDamaging = false
				
				DrownAnim:Stop(char.Info.Animations.Movement.TransitionTimes.WalkTransition.Value)
				
				if swimmingRightNow == true then
					SwimAnim:Play(char.Info.Animations.Movement.TransitionTimes.WalkTransition.Value)
				end
				
			break
		end
			
			if IsPlayingDrowningAnim == false then
				SwimAnim:Stop(char.Info.Animations.Movement.TransitionTimes.WalkTransition.Value)
				DrownAnim:Play(char.Info.Animations.Movement.TransitionTimes.WalkTransition.Value)
				IsPlayingDrowningAnim = true
				
			end
			
			game.ReplicatedStorage.Damage:FireServer(char.Info.Stats.Health.TotalHealth.Value/20)
			
			game.ReplicatedStorage.SpawnSplash:FireServer(char:WaitForChild("HumanoidRootPart"))
		
	end
	end
end



while task.wait(0.1) do
	
	
	if char:WaitForChild("Humanoid").Health <= 0 then
		task.cancel(DrownLoop())
		break
	end	
	
	if char.IsSwimming.SwimObject.Value ~= nil then
	
		LockYAxis.MaxForce = Vector3.new(0, math.huge, 0)

		LockYAxis.Position = Vector3.new(0, char.IsSwimming.SwimObject.Value.Position.Y + (char.IsSwimming.SwimObject.Value.Size.Y / 2) + char.IsSwimming.SwimVerticleOffset.Value, 0)
		
		if char:WaitForChild("HumanoidRootPart").Position.Y <= char.IsSwimming.SwimObject.Value.Position.Y + (char.IsSwimming.SwimObject.Value.Size.Y / 2) + char.IsSwimming.SwimVerticleOffset.Value and swimmingRightNow == false and char.InWater.Value == true then
			
			if char.IsSwimming.Value == false then
				game.ReplicatedStorage.ChangeBoolValue:FireServer(char.IsSwimming, true)
			end
			
			if char.IsRunning.Value == false then
				game.ReplicatedStorage.ChangeBoolValue:FireServer(char.IsRunning, true)
			end
			
			swimmingRightNow = true
			LockYAxis.Parent = char.HumanoidRootPart
			char.CharScripts.WalkAndIdleAnimation.Disabled = true
			char:WaitForChild("Humanoid").WalkSpeed = char.Info.Stats.Movement.SwimSpeed.Value
			stopAllAnimations()
			
			if char.Info.Stats.Stamina.DrainStam.Value == false then
				game.ReplicatedStorage.ChangeBoolValue:FireServer(char.Info.Stats.Stamina.DrainStam, true)
			end
			if char.Info.Stats.Stamina.GainStam.Value == true then
				game.ReplicatedStorage.ChangeBoolValue:FireServer(char.Info.Stats.Stamina.GainStam, false)
			end
			wait()
			SwimAnim:Play(char.Info.Animations.Movement.TransitionTimes.WalkTransition.Value)
			
			print("IsSwimming")
		
		elseif char:WaitForChild("HumanoidRootPart").Position.Y > char.IsSwimming.SwimObject.Value.Position.Y + (char.IsSwimming.SwimObject.Value.Size.Y / 2) + char.IsSwimming.SwimVerticleOffset.Value and swimmingRightNow == true then
			swimmingRightNow = false
			LockYAxis.Parent = nil
			SwimAnim:Stop(char.Info.Animations.Movement.TransitionTimes.WalkTransition.Value)
			DrownAnim:Stop(char.Info.Animations.Movement.TransitionTimes.WalkTransition.Value)
			char.CharScripts.WalkAndIdleAnimation.Disabled = false
			char:WaitForChild("Humanoid").WalkSpeed = char.Info.Stats.Movement.WalkSpeed.Value
			
			if char.Info.Stats.Stamina.DrainStam.Value == true then
				game.ReplicatedStorage.ChangeBoolValue:FireServer(char.Info.Stats.Stamina.DrainStam, false)
			end
			if char.Info.Stats.Stamina.GainStam.Value == false then
				game.ReplicatedStorage.ChangeBoolValue:FireServer(char.Info.Stats.Stamina.GainStam, true)
			end
			if char.IsRunning.Value == true then
				game.ReplicatedStorage.ChangeBoolValue:FireServer(char.IsRunning, false)
			end
			if char.IsSwimming.Value == true then
				game.ReplicatedStorage.ChangeBoolValue:FireServer(char.IsSwimming, false)
			end
			print("IsNotSwimming")
		
		end
		
	end
		
	if char.Info.Stats.Stamina.CurStamAmnt.Value <= 0 and swimmingRightNow == true and IsDamaging == false then
		task.spawn(DrownLoop)
	end
	
end

I cannot figure out what is causing this for the life of me. Ive tried seemingly every option and cannot figure out the cause.

is it possible that the remote events are being fired multiple times in a short period of time? Maybe add a print inside of the server inside of the remote handler and see how many times it prints.

1 Like