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.
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
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.
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
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