Limiting a NumberValue for ammo

  1. I am creating an ammo resupply box for a rocket launcher. The rocket launcher uses a NumberValue called “AmmoLeft” to determine how many rockets are left. The player cannot have more than 3 rockets at a time.

  2. The current script I have works, but with no 3 rocket limit. When I click the ammo crate, it adds a rocket to the AmmoLeft value, as you can see in the output here:

However as I mentioned before I only want the player to have a maximum of 3 rockets, because currently the player can click the ammo box over and over and stockpile as many rockets as they want, which will be far too OP in my game.

To create this limit, here is what I have tried:

My logic here was that the script will only add a rocket if AmmoLeft is less than 3, the maximum amount of rockets. If AmmoLeft is equal to three, then no rockets should add.

However, it seems once I add “and tool.AmmoLeft.Value < 3” to Line 3, the rockets do not add to the AmmoLeft value, regardless of what AmmoLeft value is.

As you can see from this picture, the value of AmmoLeft is 2, which is obviously less than 3.

However upon clicking the crate, which worked fine before I added “and tool.AmmoLeft.Value < 3”, nothing is added to AmmoLeft Value, even if it is less than 3.

I also tried adding a MaxAmmo Number Value and setting it to 3, then just setting AmmoLeft’s value to be equal to MaxAmmo upon click. Once again, nothing happens and the output is empty.

There is nothing appearing in the output. I am confused as to why it will not add a rocket like it did before when all I added was “and tool.AmmoLeft.Value < 3”.

1 Like

Wow… I really cant understand whats happening.
I tried to replicate it.
I placed a Part in workspace, with a Script and a ClickDectector, with this:

local ammoCrate = script.Parent.ClickDetector

local function addAmmo(ply)
	for _, w in pairs(ply.Backpack:GetChildren()) do
		if w.Name == "RPG" and w:FindFirstChild("RPGLauncher") then -- Whats this folder?
			print("Current Ammo: ", w.CurrentAmmo.Value)
			if w.CurrentAmmo.Value < 3 then
				print("added 1 more")
				w.CurrentAmmo.Value += 1
				print("No more ammo for you")

I created a tool in backpack, with an IntValue, named CurrentAmmo, with zero value.
Once it reaches 3, stops adding ammo…
Its working fine. And I really cant find the problem you are having. I typed my script in a different order than yours, but its essencially the same, I dont understand it, yours should work too…
Maybe you could share a roblox file to check it? theres some tiny detail that we’re not noticing

You can use math.clamp(number, min, max) to make sure it never exceeds 3.

tool.AmmoLeft.Value = math.clamp(tool.AmmoLeft.Value + 1, 0, 3)