How do I stop the reload function from running when the player unequips the tool?

I’m trying to make a gun reload system where when the player unequips the gun, the gun stops reloading.

Currently it just keeps going until the gun reaches full ammo even when the player unequips the gun, but I want to make it so it completely stops reloading when the gun is unequipped.

local function reload()
	if reloading then return end
	
	reloading = true
	
	while ammoLeft ~= maxAmmo do
		wait(1)
		ammoLeft += 1
		ammoEvent:FireClient(player, ammoLeft) --This is to do with the ammo gui
	end
	
	reloading = false
end

I’ve already got an unequipped function setup for the ammo gui which is connected to tool:Unequipped so I assume I’ll need to put something in that function to achieve my goal.

local function onUnequipped()
	ammoEvent:FireClient(player) --This is to do with the ammo gui
end

EDIT: I know I could check every round of the loop if the tool is equipped, but I want it to stop as soon as the tool is unequipped. The best comparison I can think of to what I want was if a script was running and you deleted the script, it would stop running immediately. I should have clarified this in my original post.

I can post any more code or screenshots if I need to. Any and all help will be very much appreciated.

3 Likes

Just add a variable to check whether a tool had been equipped or not, set this variable to false when the tool is unequipped, and to true when equipped.

if reloading or not Equipped then return end
1 Like

You can easily set a variable in your script like:

local equiped = false; --Bool

then in your loop you could insert a line:

local function reload()
	if reloading then return end
	
	reloading = true
	
	while ammoLeft ~= maxAmmo do
		if equipped then
			break
		end
		wait(1)
		ammoLeft += 1
		ammoEvent:FireClient(player, ammoLeft) --This is to do with the ammo gui
	end
	
	reloading = false
end

When the weapon is equipped, set the variabled to true, and when it’s not, set it to false.

1 Like

@Pr0pelled @amadeupworld2
Sorry, I made an edit to my original post:

I know I could check every round of the loop if the tool is equipped, but I want it to stop as soon as the tool is unequipped . The best comparison I can think of to what I want was if a script was running and you deleted the script, it would stop running immediately. I should have clarified this in my original post.

(sorry if you got multiple notifications I’m not very good with this site lol)

That shouldn’t be a problem if you were using @Pr0pelled’s solution.

While you’re reloading, check that the gun equipped variable is still true, and if it isn’t, break the loop.

But if the player unequips the gun after the check but before the next bullet is reloaded, the next bullet will get reloaded while the gun is unequipped.

You could just run the check after the wait, or you could leave it. In my opinion, that’s negligible.

1 Like

But no matter where I put the check there will be a point where the player can unequip the gun and have it reload another bullet, which is why this solution won’t work. Putting a check was one of the things I originally tried (yes I should’ve put that in my original post) and I’ve tried a few other solutions, but I can’t find or think of any other solution, which is why I posted here.

If you place the check, after the wait(1), there is no opportunity for the player to unequip the weapon and still add a unit of ammunition.

local function reload()
	if reloading then return end
	
	reloading = true
	
	while ammoLeft ~= maxAmmo do
		wait(1)
		if equipped then
			break
		end
		ammoLeft += 1
		ammoEvent:FireClient(player, ammoLeft) --This is to do with the ammo gui
	end
	
	reloading = false
end

Here’s how the loop goes:

  1. Wait 1 second.
  2. Check if the weapon is still equipped.
  3. Add a unit of ammunition
  4. Repeat.

Regardless of when the weapon is unequipped, it will have the same result:
EX:

  1. Wait 1 second
  2. User unequips the weapon
  3. Check if weapon is still equipped (false)
  4. Break loop

EX 2:

  1. Wait 1 second
  2. Check if weapon is still equipped (true)
  3. User unequips the weapon
  4. Unit of ammo added.
  5. Repeat.
  6. Wait 1 second.
  7. Check if weapon is equipped (false)
  8. Break loop

In example 2, the time between steps 2 and 4 is a fraction of a second, and it would be a miracle for the user to unequip the weapon during that time. If the user happens to do that, it would be a negligible, as the appropriate time has already been waited, and the weapon should have a unit of ammo added regardless.

1 Like

Thank you for explaining it. My nearly 3am brain completely missed this. I probably should’ve waited until the morning.