Gun serverside optimization

I’m working on a hitscan gun system, but I’m starting to notice that once the server memory starts to increase the delay between the client detecting a bullet hit and the server dealing damage to the player that got hit. I was wondering if this was because of potential inefficiency in my server code or if this is a separate issue.

If this is a separate issue, can somebody please give me a suggestion on how to alleviate some of the delay?

Here is the server side code:

Remotes.GunFire.OnServerEvent:Connect(function(Player,Tool,equippedWeaponRegister, equippedWeaponInfo,HitPart,Init,End, hitPos, normal)
	if Tool and Tool.Parent and Tool.Parent == Player.Character then
		if Tool:FindFirstChild("Barrel") then
			local Damage = equippedWeaponInfo.HitDamage
			if Tool:FindFirstChild("Flags") and Tool:FindFirstChild("Flags"):GetAttribute("CanFire") == false then
				return
			end
			
			if Tool.Handle:FindFirstChild("Fire") and equippedWeaponRegister.SoundDebounce ~= true then
				FireToAllOneException(Remotes.ReplicateSoundToClients, Player, Tool.Handle, Tool.Handle.Fire)
			end


			if HitPart then
				local Humanoid = GetHumanoid(HitPart)

				if equippedWeaponInfo.OnHitModify then
					if Humanoid then
						WeaponInfo[Tool.Name].OnHitModify(Tool, WeaponInfo[Tool.Name], equippedWeaponRegister, hitPos, Humanoid)
					else
						WeaponInfo[Tool.Name].OnHitModify(Tool, WeaponInfo[Tool.Name], equippedWeaponRegister, hitPos, nil)
					end
				end

				if Humanoid then
					FireToAllOneException(RenderEvent, Player, hitPos, Init, equippedWeaponInfo.BulletColor, equippedWeaponInfo.BulletColor2, nil)
					local PlayerHurt = game:GetService("Players"):GetPlayerFromCharacter(Humanoid.Parent)

					if PlayerHurt then
						if PlayerHurt.Character:FindFirstChild("ForceField") then
							return
						end
						if Damage > 0 and Humanoid.Health > 0 then
							if Tool:FindFirstChild("Flags") and Tool:FindFirstChild("Flags"):GetAttribute("TeamKill") == false and Player:FindFirstChild("teamKill") and Player:FindFirstChild("teamKill").Value == false then
								if PlayerHurt.Team == Player.Team then
									return
								end

								if Player.leaderstats:FindFirstChild("Damage") then
									Player.leaderstats:FindFirstChild("Damage").Value += Damage
								end
							end
						elseif Damage < 0 and Humanoid.Health < 100 and Humanoid.Health > 0 then
							if PlayerHurt.Team ~= Player.Team then
								return
							end

							if Player.leaderstats:FindFirstChild("Healing") then
								Player.leaderstats:FindFirstChild("Healing").Value -= Damage
							end
						end
						
					end
					--[[
					if DamageType == "Critical" then

						Remotes.NumericText:FireClient(Player,
							HitPart.Parent.HumanoidRootPart.CFrame,
							Damage,
							"-3"
						)
					else

						Remotes.NumericText:FireClient(Player,
							HitPart.Parent.HumanoidRootPart.CFrame,
							Damage,
							"-2"
						)
					end
					]]
					if Humanoid.Health > 0 then
						
						local creator = Instance.new("ObjectValue")
						creator.Name = "creator"
						creator.Value = Player
						creator.Parent = Humanoid
						Debris:AddItem(creator,0.7)
						
						

						--local DamageDealtVal = Player:FindFirstChild("leaderstats").DamageDealt
						--DamageDealtVal.Value = DamageDealtVal.Value + Damage

						Humanoid.Health=Humanoid.Health-Damage
						
						

					end
				else
					FireToAllOneException(RenderEvent, Player, hitPos, Init, equippedWeaponInfo.BulletColor, equippedWeaponInfo.BulletColor2, normal)
				end
			else
				FireToAllOneException(RenderEvent, Player, hitPos, Init, equippedWeaponInfo.BulletColor, equippedWeaponInfo.BulletColor2, nil)

			end
		end
	end
end)

I’ve never seen memory issues, maybe there is some other issue? Maybe look at CPU usage?

unrelated but some malicious client could send any part to HitPart, pretty much leading to an aimbot

I know of that and I don’t plan to patch that as this gun system is used for a training game. Any use of it is often supervised by an administrator.