2 players sharing values

So I’ve been working on this gun system that has a bug that I’ve never encountered before. If two players are in the server, They share the same exact ammo values.

Some things to note:

  • This is happening on the SERVER. This is not an issue with GUI
  • The script is located in the tool itself. So no shenanigans with it being in ServerScriptService
  • Values use variables instead of IntValues

Heres the reloading code:

if reloading then return end
	

	
	
		
	local ammoAllowed = ammo + 2 -- Ammo amount that allows them to shoot again, If this exceeds the max then it will just let them shoot at the max	
		if shooting == false then
			if ammo < maxAmmo then
				if equipped == true then
				
					local char = script.Parent.Parent
					local plr = game.Players:GetPlayerFromCharacter(char)
				
					task.wait(1)
					
					

					reloading = true


					canShoot = false
				
					

					while ammo < maxAmmo and reloading == true do

						if reserve == 0 then break end

						ammo += 1
						reserve -= 1
				
						print(plr.Name.." has "..ammo.." ammo and "..reserve.." reserve ammo")

						ammoEvent:FireClient(plr, ammo, reserve)

						if ammo == ammoAllowed then
							canShoot = true
						end

						if ammo == maxAmmo then
							reloading = false
							canShoot = true
						end

						task.wait(0.5)
					
					
				end -- End of while loop
			end
		end
	end

If you can help with this issue, I’d really appreciate it. Also, If this code is messy or does something inefficently, Please let me know. Ty <3

Move all player-specific state inside the equipped event or the reload function itself. Each player needs their own isolated version of ammo, reserve, etc.

local tool = script.Parent

tool.Equipped:Connect(function()
	local player = game.Players:GetPlayerFromCharacter(tool.Parent)
	if not player then return end

	local ammo = 0
	local reserve = 10
	local maxAmmo = 6
	local canShoot = true
	local reloading = false

	local function reload()
		if reloading or ammo >= maxAmmo or reserve == 0 then return end
		reloading = true
		canShoot = false

		while ammo < maxAmmo and reserve > 0 do
			ammo += 1
			reserve -= 1
			print(player.Name.." has "..ammo.." ammo and "..reserve.." reserve ammo")
			ammoEvent:FireClient(player, ammo, reserve)

			if ammo >= 1 then
				canShoot = true
			end

			task.wait(0.5)
		end

		reloading = false
	end

	-- bind this function to your input system
	-- Like reload()
end)

Thank you random stranger on the internet! I feel so dumb.

So I’d like to say something about the script.

Crepz’s solution didn’t really fix the problem, But it certanly got me on the right path.
The actual problem was that I was using remote events instead of doing everything from the server.

The soulution Crepz gave me was good, But it didn’t work in my script because I needed the ammo to be used elsewhere like when the player was firing there gun.

So yeah, I was kinda dumb for just not doing it on the server. And I don’t blame crepz for giving me that solution, I didn’t give much information to work with. but thanks anyway Crepz!