Ammo Box Help (value and basic scripting)

I want to make my ammo box script give ammo to a certain gun detected in the player’s inventory if it has a value named AmmoType with a value of “5.56”. The thing is that the ammo box script only gives ammo depending on the gun’s name. (for example it would give a tool named Gun +50 ammo if it had a MaxAmmo Value). I don’t know how to implement this value detecting thing into this code.

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 + 40
		
		
		script.Parent:Destroy()
		script.Parent.SoundPart.AmmoSound:Play()
		end
end)
1 Like

Would it be giving ammo for all tools, or just the 1 equiped, and the

		script.Parent:Destroy()
		script.Parent.SoundPart.AmmoSound:Play()

destroys the script before the sound is played, and it also destroys the sound.

oh
i never noticed that. To answer your first question, itll just be giving the ammo to all the guns detected in the player’s inventory to have a specific value pertaining to the gun’s ammo type. Like a pistol would have a value that says “9MM” and a rifle would have the value of “5.56”

To get all tools in the backpack and add the ammo you would just need to get the children of the backpack and add ammo to the children like this:

for i,tool in pairs(Player.Backpack:GetChildren()) do
tool.MaxAmmo.Value = tool.MaxAmmo.Value + 50
end

That should work, if it doesn’t tell me what the errors are.
Ok, I fixed it try now.

so itll add 50+ ammo to every tool in the inventory detected to have a MaxAmmo Value?

Yes, the “i,tool in pairs” gets all the children, then it adds 50 to the maxammo value.
Edit:But that’s assuming every tool has a maxammo value.

it doesn’t seem to work when i add it to my code. yet there are no errors in the output

script.Parent.ClickDetector.MouseClick:Connect(function(Player)
	
	for i,tool in pairs(Player.Backpack:GetChildren()) do
		tool.MaxAmmo.Value = tool.MaxAmmo.Value + 50
	
		script.Parent.SoundPart.GrabSound:Play()
		wait(0.9)
		script.Parent:Destroy()
		
		end

Erm…did you have the tool equiped? Or was it just unequiped in the backpack?

it was unequipped. the ClickDetector wont work if the tool is equipped. the gun would also fire if i equipped it and tried to grab ammo

Ok give me a minute, I’m gonna try running it.

script.Parent.ClickDetector.MouseClick:Connect(function(Player)

	for i,tool in pairs(Player.Backpack:GetChildren()) do
		tool.MaxAmmo.Value = tool.MaxAmmo.Value + 50

		script.Parent.SoundPart.GrabSound:Play()
		wait(0.9)
		script.Parent:Destroy()
	end
end) --forgot to add an end to the click detector function

it still doesn’t seem to work. same problem. i can’t unequip my gun and click it to obtain more ammo

would a video showing what happens help?

Before increasing the value of the MaxAmmo, I would suggest checking if the tool has that object in the first place as to prevent any errors:

for _,tool in ipairs(Player.Backpack:GetChildren()) do

    local MaxAmmo = tool:FindFirstChild("MaxAmmo")

    if tool:IsA("Tool") and MaxAmmo then

        MaxAmmo.Value += Number
    end
end

it seems to cause an error when I put an “end” at the end of the script

There has to be an end for the if statement, and and end) after that for the click function, and I’ve got no clue why it isn’t working.

Could you send an image of what the tool looks like?

tool stuff

And the part with the click detector and script?

ammobox stuff