I have a few guns that are reloadable, and they all work on one remote event. However, the remote event is received by all of the gun scripts, and it reloads all of the guns at once, doing all of the gun reload sounds at once.
How do I make sure only one gun gets reloaded, and it’s the one that sent the reload event? One idea I had was to make a client side variable with the gun name when firing the remote and having the server reload that gun, but that’s easily exploitable, because then you can reload any gun you want without even having it equipped.
Currently it works like so:
Client with gun equipped sends remote event, reloads locally > server gets remote event and reloads gun, except the server gets the reload event on all of the gun scripts.
Ok, so this is a pretty easy thing to do all we have to do is add some parameters and add some logic, I am going to give two examples of ways you can do this.
One way is to do something like this:
ReloadEvent.OnServerEvent:Connect(function(plr, TypeOfWeapon )
if TypeOfWeapon == "PISTOL" then
-- reload PISTOL
end
end)
This way might be better in the long run if you plan to have lots of guns, as it takes use of functions in tables :
local Weapon = {
ReloadPistol = function()
-- reload pistol stuff
end,
ReloadRIfle = function()
-- reload Rifle
end
}
ReloadEvent.OnServerEvent:Connect(function(plr, NameOfWeapon )
if Weapon[NameOfWeapon] then -- Check if the Weapon Exists
Weapon[NameOfWeapon]()
else
print("Error Weapon Doesn't Exist") ---NameOfWeapon is a not a weapon
end
end)
Now to call either of these all we would need to do is:
ReloadEvent:FireServer(NameofFunction or NameOfWeapon)
@Conejin_Alt made a good point, but another thing i would recommend not doing is running the reloading animations server side, only changing the amount clip/rounds should be needed. if you were to worry about hackers, There are ways to combat this, but a simple Debounce for Eachplayer should do the trick. So for example (this is just an example, of want you could potentially do):
local Debounces = {}
ReloadEvent.OnServerEvent:Connect(function(player, NameOfWeapon )
if Debounces[player] == nil then --if the player is not already in the table
Debounces[player] = {false]
end
if Debounces[player] then -- if the debounce is true
Debounces[player] = fasle --Set it to false
---reload rounds here
Debounces[player] = true -- after reloading set the debounce to true
end
end)
I Recommend you to keep this as only one script, with my experience and all those people who warn me about events, that hackers can keep firing events, so players will reload always, causing that players can’t shoot because the reload time.