Gun doubling ammo wasted when unequipping and equipping again!

Hello!

So I wanna make a game that involves using guns.

But while testing, I realised that whenever you reload and then unequip then gun then requip it the gun uses (probally) multiplies the amount of bullets used.

The LocalScript
local tool = script.Parent
local MaxAmmo = tool:GetAttribute("MaxAmmo")
local ammo = tool:GetAttribute("Ammo")
local reloading = false
local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local ammoDisplay = playergui:WaitForChild("GunGui"):FindFirstChild("Ammo")
local ammoframe = playergui:WaitForChild("GunGui"):FindFirstChild("Frame")

script.Parent.Equipped:Connect(function(mouse)
	local function reload()
		reloading = true
		wait(1)
		ammo = MaxAmmo
		reloading = false
	end
	
	script.Parent.Activated:Connect(function()
		if ammo > 0 and not reloading then
			ammo = ammo -1
			script.Parent.GunShot:Play()
			if mouse.Target.Parent:FindFirstChild("Humanoid") then
				script.Parent.DealDamage:FireServer(mouse.Target.Parent, 20)
			end
		elseif reloading == false then
			reload()
			script.Parent.GunShot:Stop()
		end
		
		while wait() do
			ammoDisplay.Text = ammo.." / "..MaxAmmo
		end
	end)
	
	local input = game:GetService("UserInputService")
	input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= MaxAmmo then
			reload()
		end
	end)
end)

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Icon = 'rbxassetid://117431027'


mouse.Button2Down:Connect(function()

	local camera = game.Workspace.CurrentCamera
	
	camera.FieldOfView = 40

end)

mouse.Button2Up:Connect(function()

	local camera = game.Workspace.CurrentCamera

	camera.FieldOfView = 70

end)

Here’s also a video to see what’s happening: https://gyazo.com/0c50eb8b489535eca180f35cec07d0dc

I will like some help.

Thanks! :smiley:

EDIT: I just red the Instance Attributes topic on the DevHub. But I don’t think i’ve made any mistake.

I’ve encountered this problem when I first learned how to script; basically the mistake is connecting a function to an event multiple times.

When you :Connect() Events, you make a function gets called each time the events get fired.

The function that “subscribes” to an event won’t get disconnected until the object responsible for the event is destroyed.
Multiple functions can “subscribe” to an event.

Generally you might want to avoid a

event:Connect(function()
   event2:Connect(function()
   end
end

unless it’s something like tracking when a child is added and removed again

workspace.TrackObjectsFolder.ChildAdded:Connect(function(child)
     print("CHILD ADDED", child)
     child.AncestryChanged:Connect(function()
          if child:IsDescendantOf(TracksObjectsFolder) then
              print("CHILD REMOVED", child)
         end
    end
end

Therefore your solution would be taking the

script.Parent.Activated:Connect(.... end)

outside of your

script.Parent.Equipped:Connect(.... end)
2 Likes

Now I get a error:

  20:22:19.725  Players.thatrandomnoob23.Backpack.AssaultRifle.MainGun:21: attempt to index nil with 'Target'  -  Client - MainGun:21
  20:22:19.726  Stack Begin  -  Studio
  20:22:19.726  Script 'Players.thatrandomnoob23.Backpack.AssaultRifle.MainGun', Line 21  -  Studio - MainGun:21
  20:22:19.726  Stack End  -  Studio
New LocalScript
local tool = script.Parent
local MaxAmmo = tool:GetAttribute("MaxAmmo")
local ammo = tool:GetAttribute("Ammo")
local reloading = false
local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local ammoDisplay = playergui:WaitForChild("GunGui"):FindFirstChild("Ammo")
local ammoframe = playergui:WaitForChild("GunGui"):FindFirstChild("Frame")

local function reload()
	reloading = true
	wait(1)
	ammo = MaxAmmo
	reloading = false
end

script.Parent.Activated:Connect(function(mouse)
	if ammo > 0 and not reloading then
		ammo = ammo -1
		script.Parent.GunShot:Play()
		if mouse.Target.Parent:FindFirstChild("Humanoid") then
			script.Parent.DealDamage:FireServer(mouse.Target.Parent, 20)
		end
	elseif reloading == false then
		reload()
		script.Parent.GunShot:Stop()
	end

	while wait() do
		ammoDisplay.Text = (ammo).. " / "..MaxAmmo
	end
end)

local input = game:GetService("UserInputService")
input.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= MaxAmmo then
		reload()
	end
end)

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Icon = 'rbxassetid://117431027'


mouse.Button2Down:Connect(function()

	local camera = game.Workspace.CurrentCamera

	camera.FieldOfView = 40

end)

mouse.Button2Up:Connect(function()

	local camera = game.Workspace.CurrentCamera

	camera.FieldOfView = 70

end)


tool.Activated event does not return any argument, therefore the
mouse
argument in your
function(mouse)
is basically nil

To get the player’s mouse, you do

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
1 Like

Heyyy! Thank you very very much! Have a great day! :stuck_out_tongue_winking_eye:

1 Like