How could I secure my Reload remote event? [SOLVED]

Hello, I’ve been recently working on a gun system for my game, and I encountered an issue with the Reload function of my gun.

I use an attribute to store the current ammo in the gun’s mag, and I handle the Reload logic on the Server. The problem is that an exploiter can easily trigger the remote event used to activate the Reload logic whenever they want. I tried adding a cooldown to the event, but they can fire it continuously and never reload the gun, and this would be the same as having infinite ammo.

I already searched for a fix on the Dev Forum, but I couldn’t find anything helpful. Some help would be appreciated.

That’s my current code for handling the Reload logic (Server):

function Class:HandleReloadLogic()
task.wait(self.Configurations.ReloadTime)

self.object:SetAttribute("AmmoInMag", self.Configurations.MagSize)
end

Thanks in advance,
Stravine

1 Like

You need to

  1. Check whether the ammo in mag is less than mag size
  2. Store a reloading variable so they can’t reload again while reloading

So, something like this should work:

local reloading = false
function Class:HandleReloadLogic()
if not reloading then
reloading = true
task.wait(self.Configurations.ReloadTime)

self.object:SetAttribute("AmmoInMag", self.Configurations.MagSize)
reloading = false
end
end

You could also make this into an attribute if you want to.
Also, just to make it a bit more playable and real, you should probably make it so you can’t fire while reloading, which honestly would solve all your problems without doing this.

1 Like

Shouldn’t the reload delay be on the server? In addition to this change you could create an attribute named “Reloading” which represents whether or not the gun is reloading, this should also be handled by the server.

1 Like

Thanks to everybody who helped me to fix this issue, I marked @Kaid3n22 's Reply as the Solution.

This is the full code I used if anybody needs it.

function Class:HandleReloadLogic()
	if not self.isReloading and self.object:GetAttribute("AmmoInMag") < self.Configurations.MagSize then
		self.isReloading = true
		
		task.wait(self.Configurations.ReloadTime)

		self.object:SetAttribute("AmmoInMag", self.Configurations.MagSize)
		
		self.isReloading = false
	end
end