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.
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.
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.
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.
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.
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:
Wait 1 second.
Check if the weapon is still equipped.
Add a unit of ammunition
Repeat.
Regardless of when the weapon is unequipped, it will have the same result:
EX:
Wait 1 second
User unequips the weapon
Check if weapon is still equipped (false)
Break loop
EX 2:
Wait 1 second
Check if weapon is still equipped (true)
User unequips the weapon
Unit of ammo added.
Repeat.
Wait 1 second.
Check if weapon is equipped (false)
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.