Values not adding correctly

When i run a script with the following inside:
script.Parent.Triggered:Connect(function(player)
if player.Character.Money.Value > 499 then
local tool = ‘AR15’

	if player.Character:FindFirstChild(tool) or player.Backpack:FindFirstChild(tool) then
		if player.Character.Money.Value > 399 then
			player.Character.Money.Value = player.Character.Money.Value - 400
			local ammo = game.Lighting.Ammo.AR15.Magazine:Clone()
			ammo.Parent = workspace
			ammo.CFrame = script.Parent.Parent.Parent.MagSpawnPos.CFrame
		end
	else 
		if player.Character.Money.Value > 799 then
			player.Character.Money.Value = player.Character.Money.Value - 800				
			local gun = game.Lighting.AR15:Clone()
			gun.Parent = workspace
			gun.Handle.CFrame = player.Character.Torso.CFrame + Vector3.new(0,2,0)	
		end
	end
end

end)

This spawns a weapon or gives ammo depending on the circumstances. Some reason, the magazine adding script bugs out with no error, where it just access the AR15 inside of the player, and adds to a value that has sense gone down, how can i fix this. This is very annoying and makes no sense. Finaly, this is the adding script inside of the mag. it DOES add, but doesnt add from the number it is at at current time.

script.Parent.Triggered:Connect(function(part)

local person = part.Name

local workspaceper = workspace:FindFirstChild(person)

local ammo = workspaceper.AR15.TotalAmmo.Value

workspaceper.AR15.TotalAmmo.Value = workspaceper.AR15.TotalAmmo.Value + 25

print(ammo)

script.Parent.Parent:Destroy()

end)

Edit: with this if the totalammo value was at 20, it would set it to 50

Also just found out if it even hits 0 then it will just go to the next addition of 25

Are the weapons and magazines meant to be placed in game.Lighting? I think it would be better if you would place them in ReplicatedStorage (or ServerStorage), unless you have a reason to keep them in game.Lighting.
For the ammo error, try making an IF statement to check if the ammo has reached to 0, then waits for the amount of time you want the gun to take to reload the gun.
Here is an example:

script.Parent.Triggered:Conect(function(player)
   local character = player.Character
   local ammo = character.AR15.TotalAmmo.Value

   if ammo.Value == 0 then
      wait() -- how many seconds you want it to add the value
      ammo.Value += 25
      print(ammo)
   
      script.Parent.Parent:Destroy()
    end
end)

just saw this, this isnt the problem, it just never registers the change of numbers. Like if i start with 25 in there, remove all of the 25 bullets then pick up the mag that has the += 25 in it it will become 50

So if 25 ammo is gone, then 50 ammo will be added, and then if 50 ammo is gone, then 75 ammo will be added, am i correct?

yep and i have no clue why it happens

also tried changing it from a script inside of the ammo, still happens

Does the magazine already start off with 25 ammo, or starts off with 0 ammo?

starts at 25, then looses some

I dont think if this will work, but try doing this

ammo.Value = 25 -- or with quotation marks "25"

i can try starting at 0 then doing this but i need for the adding to work for picking up reserved ammo

didnt help it, still goes from 0 to 50 with the +25

It probably might be something to do with your gun script then, specifically the ammo part.

the subtraction is in a local i just realized so let me just try to fix that and see

wow im an idiot, alright i fixed it

1 Like