Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to disable a script from being executed depending on a value?

Asked by 4 years ago

I've recently come across a problem where when the player attempts to shoot a shotgun when it's still empty, reloading will not disable the debounce I've set in place in order to prevent the shotgun from firing again.

local Ammo = 2

I've set a script wide local that determines what value the ammo is.

local debounce = false

local cooldownTime = 0.7

script.Parent.Activated:Connect(function()
    if not debounce then
        debounce = true
        local shooting = game:GetService("Players").LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Shoot)

        local sound = game:GetService("StarterPack").Shotgun.shooting

        sound:Play()
        shooting:Play()
        Ammo = Ammo - 1
        wait(cooldownTime)
        debounce = false
        if Ammo <= 0 then
            debounce = true
        elseif Ammo == 1 or Ammo == 2 then
                debounce = false
        end
    end
end)

The above script is how the player shoots the shotgun.

script.Parent.Equipped:Connect(function(mouse)
    mouse.KeyDown:Connect(function(key)
        if key:lower() == "r" or key:upper() == "R" then
            Ammo = Ammo+2
        end
    end)
end)

The script above is what I'm using to refill the ammo value.

I do believe that it has to do with the 'Ammo' variable constantly being subtracted into the negatives whenever the player shoots the shotgun. Any tips, suggestions, or help is appreciated!

0
Maybe do: if Ammo <= 0 then Ammo = 0 end | This resets the ammo value? N43FGXL 169 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

I've found out my answer! In case you guys want to know how I did it, here's how:

I basically added an 'and' to my first if statement in the first section of the script determining if the ammo is less than 0.

local debounce = false

local cooldownTime = 0.7

script.Parent.Activated:Connect(function()
    if not debounce and Ammo > 0 then --//This is where I added the 'and'.
        debounce = true
        local shooting = game:GetService("Players").LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Shoot)

        local sound = game:GetService("StarterPack").Shotgun.shooting

        sound:Play()
        shooting:Play()
        Ammo = Ammo - 1
        wait(cooldownTime)
        debounce = false
    end
end)
Ad

Answer this question