Can't get "Ammo" System Working

  1. What do you want to achieve?
    Create an “Ammo” system with my grenades.

  2. What is the issue?
    The “CurrentAmmo” IntValue Isn’t Subtracting.

  3. What solutions have you tried so far?

  • Changing the name of the value and folder.
  • Using a function to subtract and add ammo.
  • Modifying the script in different ways to see if it would work.
  1. What was the solution to the problem?
    Needed to change the CurrentAmmo Variable To script.Parent.Storage.Configuration.CurrentAmmo from script.Parent.Storage.Configuration.CurrentAmmo.Value.

Screenshots

The Grenade Tool:
image

The “Storage” Folder:
image

Code

The “Server” Script:
103169

Tool = script.Parent
Remote = Tool:FindFirstChild("RemoteEvent")
Grenade = script:FindFirstChild("Grenade")
-- CurrentAmmo = script.Parent.Storage.Configuration.CurrentAmmo.Value --
CurrentAmmo = script.Parent.Storage.Configuration.CurrentAmmo
Grenade.Parent = nil
Debounce = false

function ModifyAmmo(Amount, Type)
	if Type == "Add" then
		-- CurrentAmmo += Amount --
            CurrentAmmo.Value += Amount
	elseif Type == "Subtract" then
		-- CurrentAmmo -= Amount --
            CurrentAmmo.Value -= Amount
	else
		warn("No Type Selected - Ammo")
	end
end
	
Remote.OnServerEvent:Connect(function(Player)
	-- if CurrentAmmo == 0 then --
    if CurrentAmmo.Value == 0 then
		Tool.Handle.Transparency = 1
	elseif CurrentAmmo > 0 then
		Tool.Handle.Transparency = 0
		if Debounce == false then
			Debounce = true
			local Humanoid = Player.Character:WaitForChild("Humanoid")
			local Animator = Humanoid:WaitForChild("Animator")
			local ThrowAnimation = Instance.new("Animation")
			ThrowAnimation.AnimationId = "rbxassetid://6543998754"
			local ThrowAnimationTrack = Animator:LoadAnimation(ThrowAnimation)
			ThrowAnimationTrack:Play()
			ThrowAnimationTrack.Stopped:Connect(function()
				ModifyAmmo(1, "Subtract")
				local ClonedGrenade = Grenade:Clone()
				ClonedGrenade.Parent = game:GetService("Workspace")
				ClonedGrenade.Explosion.Disabled = false
				ClonedGrenade.Position = Player.Character.RightHand.Position
				ClonedGrenade.Velocity = Player.Character.UpperTorso.CFrame.lookVector * 100
				wait(5)
				Debounce = false
			end)
		end
	else 
		warn("Unknown Error Occured - Ammo")
	end
end)

The “Client” Script:
103169

Tool = script.Parent
Player = game:GetService("Players").LocalPlayer
Remote = Tool:FindFirstChild("RemoteEvent")
GUI = Tool.Storage.GUI.GrenadeGUI

Tool.Equipped:Connect(function()
 	ClonedGUI = GUI:Clone()
    ClonedGUI.Parent = Player.PlayerGui
end)

Tool.Unequipped:Connect(function()
ClonedGUI:Destroy()
end)

Tool.Activated:Connect(function()
	Remote:FireServer(Player)
end)
Information
  • Everything works as it should except the “CurrentAmmo” IntValue, It doesn’t subtract.

  • When I change the IntValue manually in-game using the explorer, it changes the GUI and doesn’t allow me to throw the grenade (Works as it should)

  • There is no error in the output.

Any Help Appreciated!

where you have CurrentAmmo = script.Parent.Storage.Configuration.CurrentAmmo.Value just make it CurrentAmmo = script.Parent.Storage.Configuration.CurrentAmmo. You are getting the value one time and just assuming it will stay the same. Rather, in your code. change all the CurrentAmmo references to CurrentAmmo.Value because the value is dynamic and can change.

1 Like