This is my reloading code for a shotgun im making, its a tool, and I want it to cancel or stop running when the tool is unequipped, but I just don’t know how.
UIS.InputBegan:Connect(function(input, GPE)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.R then
if plyr.Character.isReloading.Value == false and h.Health > 0 and ammo ~= 2 and reloading == false then
local amount = 2 - ammo
reloading = true
plyr.Character.isReloading.Value = true
tool.MiddlePart.reload:Play()
reload:Play()
wait(0.09)
reloadpart1:Play()
wait(0.25)
opened:Play()
wait(0.25)
stayopen:Play()
wait(1.40)
stayopen:Stop()
if equipped == true then
ammo = ammo + amount
maxAmmo = maxAmmo - amount
wait()
if maxAmmo < 0 then
ammo = ammo + maxAmmo
maxAmmo = 0
end
end
plyr.Character.isReloading.Value = false
reloading = false
plyr.PlayerGui.SawedOffAmmo.background.Ammo.Text = ammo
plyr.PlayerGui.SawedOffAmmo.background.MaxAmmo.Text = maxAmmo
end
end
end
end)
You can connect and disconnect the function on the Equipped and Unequipped events of Tool.
i dont really know much about disconnecting, as i have never really had to use it, but do you have any examples of how to disconnect the function?
Basic way to connect and disconnect the connection.
local reloadCon
script.Parent.Equipped:Connect(function()
reloadCon = UIS.InputBegan:Connect(function(input, GPE)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.R then
if plyr.Character.isReloading.Value == false and h.Health > 0 and ammo ~= 2 and reloading == false then
local amount = 2 - ammo
reloading = true
plyr.Character.isReloading.Value = true
tool.MiddlePart.reload:Play()
reload:Play()
wait(0.09)
reloadpart1:Play()
wait(0.25)
opened:Play()
wait(0.25)
stayopen:Play()
wait(1.40)
stayopen:Stop()
if equipped == true then
ammo = ammo + amount
maxAmmo = maxAmmo - amount
wait()
if maxAmmo < 0 then
ammo = ammo + maxAmmo
maxAmmo = 0
end
end
plyr.Character.isReloading.Value = false
reloading = false
plyr.PlayerGui.SawedOffAmmo.background.Ammo.Text = ammo
plyr.PlayerGui.SawedOffAmmo.background.MaxAmmo.Text = maxAmmo
end
end
end
end)
end)
script.Parent.Unequipped:Connect(function()
reloadCon:Disconnect()
end)
1 Like
It isnt working
if you look up at the top right, I unequip the tool, then equip it again and the reload finishes. What is supposed to happen is that i unequip and it doesnt finish the reload. (i put your code in instead of what i had for this clip)
https://streamable.com/zly52q
1 Like
Oh. That’s a different problem than I thought you were having, my bad.
That looks like the function just didn’t finish the animation for the reload before the tool was unequipped.
its ok, thanks for the help though i always appreciate help 
The connect function returns a RBXScriptConnection. You can then use the Disconnect function of the RBXScriptConnection to stop the function assigned to the event from being called.
local inputBeganConnection
local function onInputBegan(input, GPE)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.R then
if plyr.Character.isReloading.Value == false and h.Health > 0 and ammo ~= 2 and reloading == false then
local amount = 2 - ammo
reloading = true
plyr.Character.isReloading.Value = true
tool.MiddlePart.reload:Play()
reload:Play()
wait(0.09)
reloadpart1:Play()
wait(0.25)
opened:Play()
wait(0.25)
stayopen:Play()
wait(1.40)
stayopen:Stop()
if equipped == true then
ammo = ammo + amount
maxAmmo = maxAmmo - amount
wait()
if maxAmmo < 0 then
ammo = ammo + maxAmmo
maxAmmo = 0
end
end
plyr.Character.isReloading.Value = false
reloading = false
plyr.PlayerGui.SawedOffAmmo.background.Ammo.Text = ammo
plyr.PlayerGui.SawedOffAmmo.background.MaxAmmo.Text = maxAmmo
end
end
end
end
local Tool = script.Parent
Tool.Equipped:Connect(function()
inputBeganConnection = UIS.InputBegan:Connect(onInputBegan)
end)
Tool.Unequipped:Connect(function()
if inputBeganConnection then
inputBeganConnection:Disconnect()
end
end)
Side Note:
There might be a few errors in that, writing flawless code in Dev Forums is difficult.
Learning Materials:
2 Likes
Ah nvm, I miss read the code above. You’re going to need a lot more logic to program that. This does cancel the function though like the topic says.
1 Like
I plugged it in, im guessing it cancelled the function like you said, but it didn’t stop the reloading for some reason, the ammo still gets reloaded. Im guessing im going to have to try something else.
That’s probably because the thread keeps running. If you disconnect a function the system doesn’t call that function when the event happens anymore. This means if the function is still going the code keeps running. I see you used a variable equipped. You can add code to update equipped under the Tool.Equipped and Tool.Unequipped lines. If you really want the relead animation to end you can spam if statements in the line of Wait() statements (terrible method as far as programming priciples and the DRY rule but like
).
since i want to cancel the animation when unequipping, and i am using an equipped variable i guess i can spam if statements, but like, I don’t really want to. Ill try other things, but, if worst comes to worst i’ll spam the if statements