Ammo System Problems

I want my ammo system to check if the player’s inventory has a tool with a NumberValue that has the value of MaxAmmo. If so, then it will give an amount of ammo to the MaxAmmo value.

I’ve got this set of code set up, but it only works if I have all the tools in the inventory. How do I make it universal for all tools with a MaxAmmo value?

Code:

script.Parent.ClickDetector.MouseClick:Connect(function(Player)
	if Player.Backpack:FindFirstChild("G16A1") then
		Player.Backpack:FindFirstChild("G16A1").MaxAmmo.Value = Player.Backpack:FindFirstChild("G16A1").MaxAmmo.Value + 30

	
	
		if Player.Backpack:FindFirstChild("G16ANG") then
		Player.Backpack:FindFirstChild("G16ANG").MaxAmmo.Value = Player.Backpack:FindFirstChild("G16ANG").MaxAmmo.Value + 30

			script.Parent.SoundPart.GrabSound:Play()
			
			
			if Player.Backpack:FindFirstChild("HWK16")then
		Player.Backpack:FindFirstChild("HWK16").MaxAmmo.Value = Player.Backpack:FindFirstChild("HWK16").MaxAmmo.Value + 30

				
			
			
			
		script.Parent.SoundPart.GrabSound:Play()
		wait(0.75)
		script.Parent:Destroy()
	end
	end
	end
end)

If you format your code (you can select all of it and press Alt+Shift+F) you might be able to spot the problem yourself:

script.Parent.ClickDetector.MouseClick:Connect(function(Player)
    if Player.Backpack:FindFirstChild("G16A1") then
        Player.Backpack:FindFirstChild("G16A1").MaxAmmo.Value = Player.Backpack:FindFirstChild("G16A1").MaxAmmo.Value + 30

        if Player.Backpack:FindFirstChild("G16ANG") then
            Player.Backpack:FindFirstChild("G16ANG").MaxAmmo.Value = Player.Backpack:FindFirstChild("G16ANG").MaxAmmo.Value + 30

            script.Parent.SoundPart.GrabSound:Play()

            if Player.Backpack:FindFirstChild("HWK16")then
                Player.Backpack:FindFirstChild("HWK16").MaxAmmo.Value = Player.Backpack:FindFirstChild("HWK16").MaxAmmo.Value + 30

                script.Parent.SoundPart.GrabSound:Play()
                wait(0.75)
                script.Parent:Destroy()
            end
        end
    end
end)

You should use a for i,v in pairs() do loop to complete this task.
Basically you want to loop through the Players Backpack. Check if the object found is a tool. Then check if it has the value object “MaxAmmo”.

Example:

for i,v in pairs(Player.Backpack:GetChildren()) do
   if v:IsA("Tool") then
      if v:FindFirstChild("MaxAmmo") then
         v.MaxAmmo.Value += 30
      end
   end
end

Edit: Although your issue is you are enclosing the if statements inside one another. You should consider using for i,v in pairs loops so that you don’t need to modify the code everytime you add a new gun.

2 Likes

You are enclosing the if statements inside one another, that’s the problem.
Use elseif statements instead.

so if I add elseif statements it’ll work?

It turns out that elseif statements isn’t the correct solution, my bad.
Use what IEnforce_Lawz said in the above post, you should use a for loop that iterates over the contents in the backpack, rather than using an if statements for each individual weapon.

Here’s an explanation for your problem:

if (statement) then
  -- do stuff
  if (statement) then --this only gets ran if the previous statement is true
    --do stuff again
    if (statement) then --the same happens to this one
      --and again
    end
  end
end

something like this?

if (Player.Backpack:FindFirstChild("G16A1")) then
		Player.Backpack:FindFirstChild("G16A1").MaxAmmo.Value = Player.Backpack:FindFirstChild("G16A1").MaxAmmo.Value + 30

	if (Player.Backpack:FindFirstChild("G16ANG")) then 
		Player.Backpack:FindFirstChild("G16ANG").MaxAmmo.Value = Player.Backpack:FindFirstChild("G16ANG").MaxAmmo.Value + 30
		
		if (Player.Backpack:FindFirstChild("HWK16")) then 
			Player.Backpack:FindFirstChild("HWK16").MaxAmmo.Value = Player.Backpack:FindFirstChild("HWK16").MaxAmmo.Value + 30

I don’t think you read my previous post correctly. That was an example of what you were doing, not a suggestion on how you should do it.

Let me explain to you what your code is CURRENTLY doing, prior to the changes:

  • if G16A1 exists: add ammo to that gun and check for G16ANG
    • if G16ANG exists: add ammo to that gun and check for HWK16
      • if HWK16 exists: add ammo to that gun, play a sound, and destroy the script's parent

If at any point one of the if statements is false, the rest of the code below it won’t be ran, because they are nested into each other.

You should just stop bothering with the repeated if statements and use the for loops instead. It’s much easier to work with. Read the post IEnforce_Lawz made above.

OP, you might also enjoy this article: Instance | Documentation - Roblox Creator Hub

But also:

error

i’m a complete idiot when it comes to scripting, so I’m not sure how to fix this error. I implemented the code with my script and that appears.

script.Parent.ClickDetector.MouseClick:Connect(function(Player)
	
	for i,v in pairs(Player.Backpack:GetChildren()) do
	if v:IsA("Tool") then
		if v:FindFirstChild("MaxAmmo") then
			v.MaxAmmo.Value += 30
		
		script.Parent.SoundPart.GrabSound:Play()
		wait(0.75)
		script.Parent:Destroy()

end
	end
end
end

the last end is in red

Add a ) to the last end, if it still has the error remove/add “end”'s without the bracket.

Bruh why so many ends lol

Highlight all your code and press Alt+Shift+F to reindent and see where the extra ends are.

Delete them.

Then add the ) like @IEnforce_Lawz said

i think im just dumb. even the end) has become an error

Pro tip: when your code is formatted, there should only one end per “level”.

Delete the second to last one.

1 Like

um… i’m not sure what happened but it doesn’t seem to work. It plays the sound and deletes the brick, but doesn’t add more MaxAmmo. Video showing: (1) Roblox Devpost (sorry for no uploading lmao) - YouTube

look at the right bottom corner. the ammo box doesn’t increase that +50…

edit: wait nvm, it destroys the brick too short for the script toi work

edit2: ive extended the time, yet it still doesn’t update. it seems to only work if it doesn’t get destroyed

1 Like

That’s definitely not the problem :slight_smile:

Are you sure that MaxAmmo is the right value here? That looks more like it would be called TotalAmmo or something.

I set it to maxammo. if I get rid of the " script.Parent:Destroy()", then it won’t give ammo. I’ve tried adding a “wait(0.9)”, but it still doesn’t work

Remove this end and it will work. SS attached of what it looks like when fixed.