Firing an event 100+ times per second

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)
1 Like

I think it might, but the best way to be 100% sure is to do a test with friends or other developers in the game.

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.

Personally, I’d put this post in the #help-and-feedback:code-review category instead.

2 Likes

Yeah, should be fine servers can take a lot of punishment according to a trusted source.

But I’m concerned about this parameter in the on server event about data.

Edit: But yeah I believe it’s off-topic for now but I’ll put my research here just in case

Server validation concerns and research

Edit: yeah, an additional server-side validation check will be necessary and it should be good to go :+1:

Yeah, this is a pretty complicated topic from my research maybe this should help as well just in case. Balancing client response and server validity :exploding_head:

Secure Yet Accurate Gun Raycasting? - #7 by orange451

1 Like

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

you could also use tick() as it can go to milliseconds.

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.