My gun script causes frequent server freezes that last up to 10 seconds, and in many cases eventually even crash.
With about 50 players in the server, I’m pretty sure about 1500 bullets get fired each minute, yet the server constantly freezes. The script activity doesn’t seem to rise about 2%, and I’m not really sure how to go about the “debugging”.
FireClient and FireServer is only used once per fire, so with a shotgun it will just be one fire for all the bullets in the shot.
REMOTES.Effect:FireAllClients("Shoot", player,Tool, position, newdirections,config.Blood,exa1)
I am completely lost on why the server freezes so much, and im asking whats the best way to go about fixing this? I am willing to send over full code once I have tried everything, however this is all of the main part.
function Hit(player,tool,config,shootPos,t,bulletType)
local hitChar = t[1]
local hit = t[2]
local pos = t[3]
local normal = t[4]
local hcf = t[5]
local bcf = t[6]
local re = t[7]
--print(hitChar,hit)
if t
and hitChar
and hitChar:IsA("Model")
and hitChar ~= player.Character
and hitChar.Parent == workspace.Characters
and hit and hit:IsA("BasePart")
and hit.Parent == hitChar
and pos and typeof(pos) == "Vector3"
and normal and typeof(normal) == "Vector3"
and hcf and typeof(hcf) == "CFrame"
and bcf and typeof(bcf) == "CFrame"
and re and typeof(re) == "table"
then
local hHumanoid = hitChar:FindFirstChildOfClass("Humanoid")
if hHumanoid.Health > 0 and DAMAGE:PlayerCanDamage(player, hHumanoid) then
local distance = (hit.Position - shootPos).Magnitude
local distance2 = (hit.Position - hcf.p).Magnitude
if distance > config.Range * 1.05 then return end
if distance2 > 15 then return end
--print("ye")
local damage,perc = DAMAGE:Calculate(tool, hit, pos)
if bulletType and config.BulletTypeChange and config.BulletTypeChange.Enabled and bulletType == "RB" then
damage = damage / config.BulletTypeChange[bulletType .. "_Reduction"]
if hHumanoid.Health - damage < 1 then
damage = hHumanoid.Health - 1
end
end
local wRagdolled = false
if _G.RagdollCheck and _G.RagdollCheck(hitChar) then
wRagdolled = true
end
delay(0.05,function()
if hitChar and hitChar.Parent and _G.RagdollCheck and _G.RagdollCheck(hitChar) then
local x1 = shootPos
local x2 = pos
x1 = Vector3.new(x1.X,0,x1.Z)
x2 = Vector3.new(x2.X,0,x2.Z)
local force = (CFrame.new(x1,x2).LookVector + Vector3.new(0,1.5,0)) * (config.Knockback and config.Knockback) or 1
if not wRagdolled then
force = force * 2.5
end
_G.SMains.ForceChar(hitChar,hit,force * perc,0.15,hHumanoid.Health - damage <= 0)
end
end)
return hitChar,hit,hHumanoid,damage
-- if hPlayer then
-- --REMOTES.HitIndicator:FireClient(otherPlayer, direction, damage)
-- end
--
-- if hHumanoid.Health <= 0 then
-- for _, part in pairs(hHumanoid.Parent:GetChildren()) do
-- if part:IsA("BasePart") then
-- part.Velocity = direction * config.Damage
-- end
-- end
-- --local killDist = math.floor((position - humanoid.Parent.HumanoidRootPart.Position).Magnitude + 0.5)
-- --STAT_SCRIPT.FurthestKill:Fire(player, killDist)
-- --REMOTES.Killfeed:FireAllClients(player.Name, humanoid.Parent.Name, shot.Tool.Name, killDist)
-- end
end
end
end
REMOTES.Gun_Shoot.OnServerEvent:connect(function(player, Tool, mode,position, directions,exa1,exa2)
spawn(function()
if not guns[Tool] then return end
AA_Module = _G.AntiExploitModule
if not AA_Module:CheckTing(player.Character) then return end
if shots[player] then
shots[player] = nil
end
local character = player.Character
local rootPart = character.HumanoidRootPart
local humanoid = character.Humanoid
if Tool.Parent == character
then
local Handle = Tool.Handle
local Values = Tool.Values
local ammo = Values.Ammo
local config = CONFIG:GetConfig(Tool)
if mode == "Do" then
if _G.CheckIfCan and not _G.CheckIfCan(character) then return end
if (rootPart.Position - position).Magnitude < 15 then
if ammo.Value > 0 then
if not (config.GrenadeLauncherEnabled or config.RocketLauncherEnabled) and #directions == config.BulletsPerShot then
if #directions > config.BulletsPerShot then return end
ammo.Value = ammo.Value - 1
if config.MinigunEnabled and not Handle.Muzzle.FireSound.Playing then
Handle.Muzzle.FireSound:Play()
end
_G.ZaWarudoWait()
local shot = {
Tool = Tool;
Config = config;
Position = position;
Directions = directions;
}
shots[player] = shot
cancels[Tool] = true
local dmges = {}
local newdirections = {}
for i, direction in pairs(directions) do
if direction[2] then
local hChar,hit,hHumanoid,damage = Hit(player,Tool,config,position,direction[2],exa1)
if hChar then
local l = dmges[direction[2][2].Name]
if not l then dmges[direction[2][2].Name] = {hChar,hit,hHumanoid,0};l = dmges[direction[2][2].Name] end
l[4] = l[4] + damage
if config.CanExecute and _G.DownedCheck(hChar) then
table.insert(direction[2],true)
end
end
end
table.insert(newdirections,direction)
end
for _,v in pairs(dmges) do
local hChar = v[1]
local hit = v[2]
local hHumanoid = v[3]
local damage = v[4]
if _G.DownedCheck ~= nil and _G.DownedCheck(hChar) then
if config.CanExecute then
--damage = damage / 4
DAMAGE:Damage(hHumanoid, damage / config.DownedDiv, player)
end
else
DAMAGE:Damage(hHumanoid, damage, player)
if _G.FlinchChar then
local x = not (config.RocketLauncherEnabled or config.GrenadeLauncherEnabled)
_G.FlinchChar(hChar,hHumanoid,config.FlinchTime or 0,false,x)
end
if _G.TagChar then
_G.TagChar(hChar,player,15,0)
end
end
end
--REMOTES.Effect:FireAllClients("Shoot", player,Tool, position, newdirections,config.Blood,exa1)
FireClientFunc("Shoot", player,Tool, position, newdirections,config.Blood,exa1)
local c = math.random()
sCodes[Tool] = c
wait(1)
if sCodes[Tool] == c and config.MinigunEnabled and Handle.Muzzle.FireSound.Playing then
Handle.TriggerA_Server.WindUp:Stop()
Handle.TriggerA_Server.WindDown:Play()
Handle.Muzzle.FireSound:Stop()
Handle.Muzzle.FireStop:Play()
end