Fast Cast causing lag

Hey guys, I’ve been working on an FPS game for quite a while and I have implemented fast cast into it.
Fast cast has been amazing and I love it, but I’ve seem to have ran into an issue. During the game when you first start out the guns shoot and there’s no lag at all, but later into the game lag seems to increase when using the guns. Some things to note:

Lag does not occur when shooting the gun at a wall or something that’s not rendering a hit. It seems that the lag is occurring at the hit phase of shooting when it communicates with the server side script.

Has anyone had this problem, or does anyone know a solution to prevent this lag?

Thank you in advance!

--fastCastHandler client module
function rayHit(hitPart, hitPoint, normal, material, bullet)
	bullet:Destroy()
	--print(hitPart)
	if not hitPart then return end
	
	-- algorithm for finding humanoids
	-- maybe not the most efficient? i'm not sure
	-- doesn't work the best with accessories, headshots arent detected through hats
	local pumpkinHealth
	local pumpkin = false
	local humanoid --= hitPart:FindFirstChild("Zombie")
	local curParent = hitPart
	local headshot = false
	
	--if humanoid then print ("humanoid") end
	repeat
		
		if curParent.Name == "Head" then
			headshot = true
		end
		
		curParent = curParent.Parent
		humanoid = curParent:FindFirstChild("Zombie")
		pumpkinHealth = curParent:FindFirstChild("PumpkinHealth")
	until curParent == workspace or humanoid or pumpkinHealth
	
	if pumpkinHealth then
		print("Pumpkin Health True")
		local headshotSound = weaponAssets.Sounds.Headshot:Clone()
		headshotSound.Parent = players.LocalPlayer.Character
		pumpkin = true
		headshotSound:Play()
		game:GetService("Debris"):AddItem(headshotSound,1)
	end
	
	if not humanoid and not pumpkin then
	--	print(hitPart)
		
	else
		if headshot == true then
			local headshotSound = weaponAssets.Sounds.Headshot:Clone()
			headshotSound.Parent = players.LocalPlayer.Character
			headshotSound:Play()
			--local hitmarkerRed = players.LocalPlayer.PlayerGui.HitMarker.HitMarkerRed:Clone()
			--hitmarkerRed.Visible = true
			--hitmarkerRed.Parent = players.LocalPlayer.PlayerGui.HitMarker
			game:GetService("Debris"):AddItem(headshotSound,1)
			--game:GetService("Debris"):AddItem(hitmarkerRed,0.3)
		else
			local hitMarker = weaponAssets.Sounds.HitMarker:Clone()
			hitMarker.Parent = players.LocalPlayer.Character
			hitMarker:Play()
			game:GetService("Debris"):AddItem(hitMarker,1)
		end
		effects:hitBox(hitPart,damage)
		replicatedStorage.weaponRemotes.hit:FireServer(humanoid,headshot,pumpkin,pumpkinHealth)
	end
	
end
-- Server sided hit register
remotes:WaitForChild("hit").OnServerEvent:Connect(function(player, humanoid, headshot, pumpkin, pumpkinHealth)
	local weaponTable = players[player.UserId]
	print(weaponTable)
	if not players[player.UserId].currentWeapon then return end
	if not player.Character then return end
	if weaponTable.magData[weaponTable.currentIndex].current > 0 then

		print("Pumpkin ".. tostring(pumpkin))
		if pumpkin then
			pumpkinHealth.Value = pumpkinHealth.Value - players[player.UserId].currentWeapon.settings.firing.damage
			return
		end
		local damagedBy = humanoid.Parent:FindFirstChild("DamagedBy")
		if not damagedBy:FindFirstChild(player.Name) then
			local tag = Instance.new("StringValue")
			tag.Name = player.Name
			tag.Parent = damagedBy
			tag.Value = player.Name
		end
		--[[if (player.Character:WaitForChild("HumanoidRootPart").CFrame.p - humanoid.Parent.Torso.CFrame.p).magnitude < 7 then
			remotes.ScreenBlood:FireClient(player)
			print("fired")
		end--]]
		if headshot then
			humanoid:TakeDamage(players[player.UserId].currentWeapon.settings.firing.headshot)
		else
			humanoid:TakeDamage(players[player.UserId].currentWeapon.settings.firing.damage)
		end
	end
end)

Does lag increase based off of the number of guns present or based off of the amount you have shot? I haven’t used fast cast whatsoever, but it sounds like this might be an issue with stacking calls to fire

It seems to be an issue with the amount shot.

I have a shotgun weapon that shoots about 8 projectiles at a time. It seems that sometimes when that weapon is used, that’s what starts the lag up.

Is it the same amount of lag if the shotgun is shot the first time or the n:th time? (Does it lag more the more you’ve shot it?)

It is the same amount of lag each time the shotgun is shot, but once the lag starts up, it lags for every gun afterwards when it is shot. It seems to be more pronounced the more shots registered. I have edited my post to include the fastCastHandler which registers the hit on the client side, and the server side gun control for when the fastCastHandler fires the server.

Is there an increase in calls to rayHit() when the lag starts up?

I can’t seem to replicate the problem. But thank you for your help! I will add some print statements to see if I can debug it when it happens again!

–Stuff for server side hit register
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local remotes = replicatedStorage:WaitForChild(“weaponRemotes”)

–fastCast client module
function rayHit(hitPart, hitPoint, normal, material, bullet)
bullet:Destroy()
–print(hitPart)
if not hitPart then return end

-- algorithm for finding humanoids
-- maybe not the most efficient? i'm not sure
-- doesn't work the best with accessories, headshots arent detected through hats
local pumpkinHealth
local pumpkin = false
local humanoid --= hitPart:FindFirstChild("Zombie")
local curParent = hitPart
local headshot = false

--if humanoid then print ("humanoid") end
repeat
	
	if curParent.Name == "Head" then
		headshot = true
	end
	
	curParent = curParent.Parent
	humanoid = curParent:FindFirstChild("Zombie")
	pumpkinHealth = curParent:FindFirstChild("PumpkinHealth")
until curParent == workspace or humanoid or pumpkinHealth

if pumpkinHealth then
	print("Pumpkin Health True")
	local headshotSound = weaponAssets.Sounds.Headshot:Clone()
	headshotSound.Parent = players.LocalPlayer.Character
	pumpkin = true
	headshotSound:Play()
	game:GetService("Debris"):AddItem(headshotSound,1)
end

if not humanoid and not pumpkin then
--	print(hitPart)
	
else
	if headshot == true then
		local headshotSound = weaponAssets.Sounds.Headshot:Clone()
		headshotSound.Parent = players.LocalPlayer.Character
		headshotSound:Play()
		--local hitmarkerRed = players.LocalPlayer.PlayerGui.HitMarker.HitMarkerRed:Clone()
		--hitmarkerRed.Visible = true