Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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!

1 comment

0

Maybe do: if Ammo <= 0 then Ammo = 0 end | This resets the ammo value?

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.