I’m making a gun system, and I want to fire an event every time a bullet is shot, or a player is hit, or if a player tries to refill ammo, in case 10 people fire at the same time, the server might receive a lot of events at once, will that cause lag? as the script that handles the remote event is this:
local Debris = game:GetService("Debris")
local Event = game.ReplicatedStorage.GInfo
Event.OnServerEvent:Connect(function(Player, IT, Data)
if IT == "Damage" then
if Data.Humanoid and Data.Damage and (Data.Damage < 50) then
if Data.Humanoid.Parent then
local Damage = Data.Damage
if (Data.HitPart.Name == "Head") then
if Data.Humanoid.Parent:FindFirstChild("_Helmet") then
Damage = Damage * 0.6
end
elseif Data.HitPart.Name:lower():match("torso") or (Data.HitPart.Name == "HumanoidRootPart") then
Damage = Damage * 0.8
if Data.Humanoid.Parent:FindFirstChild("_Armor") then
Damage = Damage * 0.6
end
else
Damage = Damage * 0.5
end
Data.Humanoid.Health = Data.Humanoid.Health - Damage
end
end
elseif IT == "Fire" then
if Data.Gun and Data.Gun.Parent == Player.Character then
if Data.Gun.LoadedAmmo.Value > 0 then
Data.Gun.LoadedAmmo.Value = Data.Gun.LoadedAmmo.Value - 1
local Bullet = Instance.new("CFrameValue", workspace.GlobalBullets)
Bullet.Name = Data.Velocity
Debris:AddItem(Bullet, 2)
end
end
elseif IT == "Reload" then
if (Data.UnloadedAmmo.Value > 0) and (Data.LoadedAmmo.Value < Data.MaxLoadedAmmo.Value) then
local Amount = math.min(Data.MaxLoadedAmmo.Value - Data.LoadedAmmo.Value, Data.UnloadedAmmo.Value)
Data.UnloadedAmmo.Value = Data.UnloadedAmmo.Value - Amount
Data.LoadedAmmo.Value = Data.LoadedAmmo.Value + Amount
Event:FireClient(Player) -- // Refresh the ammo for the player on his local side.
end
end
end)
Should be no issue. Remotes themselves aren’t that expensive, how you handle them is probably way more important to look at. Remember to construct all visuals on the client and interface with the logic on the server.
firing a event 100x per second sounds like it will cause lag so why not just firing 2 events. One when they click and one where they stopped clicking.
You can add this for example
local Event = -- Path to event
local isPressed = true
while isPressed = true do
Event.OnServerEvent:Connect(function()
isPressed = false
return
end)
--your code here
end
so you use the events to change the value which will start and stop the lool
The guns are client sided to avoid delay, verification is done on server, plus to show bullets to the rest of the players, the server must tell them that the player has shot, it also verifies ammo to prevent inf ammo.
I don’t use straight raycasting, the gun’s bullets calculate physics, If I keep verifying the physics on the server that will probably cause massive lag, I’ll just try to prevent noclipping, Inf. ammo instead and very fast gun firing instead.