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)