Ammo Not Subtracting

Hello, Everytime the Server receives the Remote Event it is supposed to subtract from the Number Value in the script named “Ammo” until it gets to 0. Instead, when it prints the ammo value it keeps printing 29 (max ammo is 30). I’m not sure why it isn’t working thanks for any help.

local rep = game:GetService("ReplicatedStorage")




local tool = script.Parent




local remoteEvent = game.ReplicatedStorage["Weapon RemoteEvents"]["STAR-TR"]
characther = nil




remoteEvent.OnServerEvent:Connect(function(plr, mousePos, plrStrength)
	local ammo = plr.Character:FindFirstChildWhichIsA("Tool"):FindFirstChild("CurrentAmmo").Value
	if ammo >= 1 then
		ammo = ammo - 1
		print(ammo)
		characther  = plr.Character or plr.CharacterAdded:Wait()




		params.FilterType = Enum.RaycastFilterType.Exclude
		params.FilterDescendantsInstances = {tool, characther, tool.Model:GetChildren(), tool.Handle}
		behavior.RaycastParams = params
		behavior.Acceleration = Vector3.new(0, gravity, 0)
		behavior.AutoIgnoreContainer = true
		behavior.CosmeticBulletTemplate = ball
		behavior.CosmeticBulletContainer = workspace.AvailableDodgeballs

		local pos = tool.Handle.CFrame * CFrame.new(0,0,-50)
		local direction = (mousePos - tool.Handle.Position).Unit


		caster:Fire(tool.Handle.Attachment.WorldPosition ,direction, speed, behavior)		
	end
end)

-- There is more code but I omitted it let me know if you need it

I believe you are experiencing this issue because of this part

local ammo = plr.Character:FindFirstChildWhichIsA("Tool"):FindFirstChild("CurrentAmmo").Value
	if ammo >= 1 then
		ammo = ammo - 1
        print(ammo)

the variable ammo is a number instead of the object holding the value, so changing the number of the variable in the script doesn’t change the objects value.

Try changing it to this:

remoteEvent.OnServerEvent:Connect(function(plr, mousePos, plrStrength)
	local ammo = plr.Character:FindFirstChildWhichIsA("Tool"):FindFirstChild("CurrentAmmo")
	if ammo.Value >= 1 then
		ammo.Value = ammo.Value - 1
		print(ammo)
		characther  = plr.Character or plr.CharacterAdded:Wait()




		params.FilterType = Enum.RaycastFilterType.Exclude
		params.FilterDescendantsInstances = {tool, characther, tool.Model:GetChildren(), tool.Handle}
		behavior.RaycastParams = params
		behavior.Acceleration = Vector3.new(0, gravity, 0)
		behavior.AutoIgnoreContainer = true
		behavior.CosmeticBulletTemplate = ball
		behavior.CosmeticBulletContainer = workspace.AvailableDodgeballs

		local pos = tool.Handle.CFrame * CFrame.new(0,0,-50)
		local direction = (mousePos - tool.Handle.Position).Unit


		caster:Fire(tool.Handle.Attachment.WorldPosition ,direction, speed, behavior)		
	end
end)

Let me know if this works so I can correct it if it doesn’t.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.