How do I stop reloading when tool is unequipped?

So I have this reloading script which runs when the reload remote event fires.

--// Variables \\--

-- Variables --
local tool = script.Parent

-- Values --
local equipped = false

local reloading = tool:WaitForChild("Reloading")

local ammoFolder = tool.Ammo
local mag = ammoFolder.Mag
local maxAmmo = ammoFolder.MaxAmmo

--// Code \\--

-- Equipping --
tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
	reloading.Value = false
end)

-- Reloading --
tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
	if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end
	
	reloading.Value = true
	
	wait(0.5)
	tool.BodyAttach.MagOut:Play()
	wait(0.53)
	tool.BodyAttach.MagIn:Play()
	wait(0.6)
	tool.BodyAttach.BoltBack:Play()
	wait(0.27)
	tool.BodyAttach.BoltForward:Play()
	wait(0.77)
	
	-- Handle Ammo --
	mag.Value = maxAmmo.Value
		
	reloading.Value = false
end)

Does anyone know how to stop reload when the player unequips the tool?

  • tell me if you need more info (i deleted everything about firing the gun just to make the code easier to understand)
1 Like

Instead of using the “or” operator here, you might consider using “and”

1 Like

add an if statement to check if the reloading value is still true.

	if reloading.Value == true then
		mag.Value = maxAmmo.Value
		
		reloading.Value = false
	end
1 Like

You can simply tie the script with a tool.equipped function so it only occurs when the tool is equipped.

1 Like

But then if the player unequips after the if statements passes if wont detect it so it won’t stop.

1 Like

I don’t think that makes a difference.

Right here, you changed the reloading value to false after unequipping the tool. So, if they started to reload, it would be turned to false and not add the ammo.

Here is what I meant with tying in a equipped function.

local activateequip;
--This is called a connection. 
activateequip = tool.Equipped:Connect(function()
-- your equip script here
end)
tool.Unequipped:Connect(function()
activateequip:Disconnect()
--when tool is unequipped, it disconnects the equipped function rendering it disabled
end)

I did this:

--// Variables \\--

-- Variables --
local tool = script.Parent

-- Values --
local equipped = false

local reloading = tool:WaitForChild("Reloading")

local ammoFolder = tool.Ammo
local mag = ammoFolder.Mag
local maxAmmo = ammoFolder.MaxAmmo

--// Code \\--
local func

-- Equipping --
tool.Equipped:Connect(function()
	equipped = true
	
	func = tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
		if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end

		reloading.Value = true

		wait(0.5)
		tool.BodyAttach.MagOut:Play()
		wait(0.53)
		tool.BodyAttach.MagIn:Play()
		wait(0.6)
		tool.BodyAttach.BoltBack:Play()
		wait(0.27)
		tool.BodyAttach.BoltForward:Play()
		wait(0.77)

		-- Handle Ammo --
		mag.Value = maxAmmo.Value

		reloading.Value = false
	end)
end)

tool.Unequipped:Connect(function()
	equipped = false
	reloading.Value = false
	
	func:Disconnect()
end)

-- Reloading --
tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
	if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end
	
	reloading.Value = true
	
	wait(0.5)
	tool.BodyAttach.MagOut:Play()
	wait(0.53)
	tool.BodyAttach.MagIn:Play()
	wait(0.6)
	tool.BodyAttach.BoltBack:Play()
	wait(0.27)
	tool.BodyAttach.BoltForward:Play()
	wait(0.77)
	
	-- Handle Ammo --
	mag.Value = maxAmmo.Value
		
	reloading.Value = false
end)

It didn’t work. (i think that’s what you meant)

Remove “equipped” and “unequipped” variables, also you need two connections as you have another function below for reloading

Also please make the connection the tool equipped function

Sorry, but can you show me how to do that. lol

Oh sorry i tried it again but with this:

--// Variables \\--

-- Variables --
local tool = script.Parent

-- Values --
local equipped = false

local reloading = tool:WaitForChild("Reloading")

local ammoFolder = tool.Ammo
local mag = ammoFolder.Mag
local maxAmmo = ammoFolder.MaxAmmo

--// Code \\--
local func

-- Equipping --
tool.Equipped:Connect(function()
	equipped = true
	
	func = tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
		if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end

		reloading.Value = true

		wait(0.5)
		tool.BodyAttach.MagOut:Play()
		wait(0.53)
		tool.BodyAttach.MagIn:Play()
		wait(0.6)
		tool.BodyAttach.BoltBack:Play()
		wait(0.27)
		tool.BodyAttach.BoltForward:Play()
		wait(0.77)

		-- Handle Ammo --
		mag.Value = maxAmmo.Value

		reloading.Value = false
	end)
end)

tool.Unequipped:Connect(function()
	equipped = false
	reloading.Value = false
	
	func:Disconnect()
end)

But I got the same results.

--// Variables \\--

-- Variables --
local tool = script.Parent

-- Values --
local equipped = false

local reloading = tool:WaitForChild("Reloading")

local ammoFolder = tool.Ammo
local mag = ammoFolder.Mag
local maxAmmo = ammoFolder.MaxAmmo

--// Code \\--
local func;

-- Equipping --
func = tool.Equipped:Connect(function()
	equipped = true
	
	tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
		if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end

		reloading.Value = true

		wait(0.5)
		tool.BodyAttach.MagOut:Play()
		wait(0.53)
		tool.BodyAttach.MagIn:Play()
		wait(0.6)
		tool.BodyAttach.BoltBack:Play()
		wait(0.27)
		tool.BodyAttach.BoltForward:Play()
		wait(0.77)

		-- Handle Ammo --
		mag.Value = maxAmmo.Value

		reloading.Value = false
	end)
end)

tool.Unequipped:Connect(function()
	equipped = false
	reloading.Value = false
	
	func:Disconnect()
end)

Try that, tell me if any errors occur because it’s quite hard to tell on the DevForum

You need to add ; after func other wise it is just a variable, not a connection

no errors it’s the same as before (i also added the ; after func)

I think its because the reloading has waits inside of it. I put them there because my sounds are all seperate

Can I ask why you have two reloading functions?

i took the other one away but the same thing happens