Reloading problem

If you use 3 guns and shoot like once, unequip all and click r all 3 guns reload at the same time.

local gun = script.Parent
local gun_shot = gun:WaitForChild('Handle').gunshot
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('AK47ShootEvent')
local shooting = false
local equipped = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local reloadsound = gun.Handle:WaitForChild('Reload')
local emptyclipsound = gun.Handle:WaitForChild('emptyClip')
local fire = gun:WaitForChild('Handle').gunshot
local clipSize = gun.Ammo.Value
local ammo = gun.Ammo
local UIS = game:GetService('UserInputService')



-- remote event
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild('PistolShootEvent')
local ammoEvent = ReplicatedStorage:WaitForChild('AmmoPickup')

-- update ammo function

local function update_ammo()
	player.PlayerGui.AmmoGUI.Ammo.Text = 'Ammo: ' ..tostring(ammo.Value)..' / '..tostring(gun.MaxAmmo.Value)
end

local mouseConnection

-- checks if mouse is clicked
gun.Equipped:Connect(function()
	mouse.Icon = "rbxassetid://117431027"
	player.PlayerGui.AmmoGUI.Ammo.Visible = true
	update_ammo()

	equipped = true
	mouse.Icon = 'rbxassetid://117431027'
	mouseConnection = mouse.Button1Down:Connect(function()
		shooting = true
		while shooting and equipped and gun.Ammo.Value > 0 do
			local Ammo = gun:WaitForChild('Ammo').Value
			remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
			gun_shot:Play()
			gun.Ammo.Value = gun.Ammo.Value - 1
			mouse.Button1Up:Connect(function()
				shooting = false
				if gun.Ammo.Value == 0 then
					emptyclipsound:Play()
				end
			end)
			wait(0.1)
		end
	end)
end)

gun.Unequipped:Connect(function()
	equipped = false
	mouseConnection:Disconnect()
end)

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)


--Unequip gun
gun.Unequipped:Connect(function()
	player:FindFirstChild('PlayerGui').AmmoGUI.Ammo.Visible = false
	mouse.Icon = "rbxassetid://117431027"
end)

-- checks if reload

UIS.InputBegan:Connect(function(input, gameprocessed)
	if not gameprocessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R then
				if gun.Ammo.Value < clipSize and gun.MaxAmmo.Value > 0 and gun.Equipped then
					reloadsound:Play()
					reloadsound.Ended:Wait()
					if gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value) >= 0 then
						gun.MaxAmmo.Value = gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value)
						gun.Ammo.Value = clipSize
					else
						gun.Ammo.Value = gun.Ammo.Value + gun.MaxAmmo.Value
						gun.MaxAmmo.Value = 0
						update_ammo()
					end
				end
			end
		end
	end
end)

-- update ammo gui

ammo.Changed:Connect(update_ammo)

ammoEvent.OnClientEvent:Connect(function()

	gun.MaxAmmo.Value = gun.MaxAmmo.Value + 50
	update_ammo()

end)

-- local unequip gun

local function gunUnequipped()
	if gun.Unequipped and gun.Parent:IsA('Backpack')then
		while true do
			wait(1)
			if gun.Equipped then
				break
			end
		end
	end
end

ammoEvent.OnClientEvent:Connect(function()
	player.GrenadeAmmo.Value = player.GrenadeAmmo.Value + 1
end)

https://cdn.discordapp.com/attachments/822790702651670529/830332258917285919/2021-04-10_14-17-53.mp4

1 Like

I believe the issue is here with the UIS input began connection which runs right as the tool is inserted into the character back pack.

Therefore when you press r to reload, this input is sent to all three guns in the backpack and reloads all three guns in the backpack simultaneously even if unequipped because the connection is still enabled when the tool is in the backpack and continuosly reading input.

To solve this use the equipped and unequipped event and a variable representing IsEquipped to tell if the current gun is equipped at the moment and can be reloaded.

How do I do that? I got confused at what to do.

looking through the post seems like you already got the equipped variable, in your equipped connection:

but you forgot to input it within the check in a bool value here

So within your if equipped statement it should be intended as:

gun.Equipped.Value = true

which tbh is unnecessary to use an additional bool value.

The main issue is in the reload function that you are not checking if the equipped value is true after the reload sound is finished to which the player could have switched weapons by then so you can put in a return to break the function like so.

UIS.InputBegan:Connect(function(input, gameprocessed)
	if not gameprocessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R then
				if gun.Ammo.Value < clipSize and gun.MaxAmmo.Value > 0 and gun.Equipped then
					reloadsound:Play()
					reloadsound.Ended:Wait()
if not equipped then
return --do nothing after this line if gun is not equipped
--after "reloading" is finished
end
					if gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value) >= 0 then
						gun.MaxAmmo.Value = gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value)
						gun.Ammo.Value = clipSize
					else
						gun.Ammo.Value = gun.Ammo.Value + gun.MaxAmmo.Value
						gun.MaxAmmo.Value = 0
						update_ammo()
					end
				end
			end
		end
	end
end)

1 Like