Reloading function not working?

I attempted at making a tool that is similar to a gun, whenever you shoot, you lose a bullet, and if the amount of bullets is zero then it reloads. The only thing is the the gun doesnt cast a ray or does damage, the reason why is because I attempted at creating a gun one time, but gave up at the reloading part, so I just made myself a tool that only has a reloading and bullet wasting function. I want it so that if the value enabled is equal to false, then the tool decides to not waste a bullet / fire the gun. I labeled where the problem is so that it is easier to find.

local tool = script.Parent.Parent

local enabled = true
local maxAmmo = tool.Handle:WaitForChild(“maxAmmo”) --intvalue

if enabled == true then

tool.Activated:Connect(function()

  maxAmmo.Value = maxAmmo.Value - 1
  if maxAmmo.Value < 1 then
  	enabled = false
  	tool.Handle.Reload:Play()
  	wait(2)
  	maxAmmo.Value = 10
  	enabled = true
  	end
  end)

elseif enabled == false then
–PROBLEM HERE (what to do here???)
–PROBLEM HERE (what to do here???)
–PROBLEM HERE (what to do here???)
end

Please help.

If you want it to do nothing when it isn’t enabled, then you don’t have to include the part and can just end it on the if statement. Nothing is going to happen because you’re only running the line of code to remove the bullets if your code is true, but it wouldn’t because it would be false.

1 Like

Something like this?

local tool = script.Parent.Parent

local enabled = true
local maxAmmo = tool.Handle:WaitForChild(“maxAmmo”)

if enabled == true then

tool.Activated:Connect(function()

  maxAmmo.Value = maxAmmo.Value - 1
  if maxAmmo.Value < 1 then
  	enabled = false
  	tool.Handle.Reload:Play()
  	wait(2)
  	maxAmmo.Value = 10
  	enabled = true
  	end
  end)

end

1 Like

If that is what you mean, then it didn’t work. It just keeps on decreasing the value of the ammo going into the negatives. It doesn’t stop the tool from decreasing the ammo.

Try moving the enabled variables inside of the Tool.Activated function, instead of around and inside it.

tool.Activated:Connect(function()

    if enabled == true then
        maxAmmo.Value = maxAmmo.Value - 1

        if maxAmmo.Value < 1 then
            enabled = false
            tool.Handle.Reload:Play()
            wait(2)
            maxAmmo.Value = 10
            enabled = true
        end
   end)
end

Oh! It worked! Thank you so much!

1 Like