AmmoDetection script won't work

I have a gun which has a script in it that detects if the NumberValue named Ammo reaches 0, once it does reach zero it should stop the tool from functioning and parent the tool to ServerStorage. However, for some reason it doesn’t work. No errors, and as far as I know the script stops working when it gets to detecting when the value reaches zero. I’m stuck on what to do and any help would be appreciated

local Tool = script.Parent
local Ammo = Tool.Ammo
local MaxAmmo = Tool.MaxAmmo
local WeaponStorage = game.ServerStorage.Weapons

Tool.Activated:Connect(function()
	if Ammo.Value <= 0 then
		Tool.Sounds.OutOfAmmo:Play()
		Tool.Enabled = false
		task.wait(0.5)
		Tool.Parent = WeaponStorage
	end
end)
4 Likes
Ammo.Changed:Connect(function(newValue)
	if newValue <= 0 then
		Tool.Enabled = false
		task.wait(0.5)
		Tool.Parent = WeaponStorage
	end
end)

Yeah this might fixed your problem

1 Like
local Tool = script.Parent
local Ammo = Tool:WaitForChild("Ammo")
local WeaponStorage = game.ServerStorage.Weapons
local SoundsFolder = Tool:WaitForChild("Sounds")
local OutOfAmmoSound = SoundsFolder:WaitForChild("OutOfAmmo")

local function checkAmmo()
	if Ammo.Value <= 0 then
		OutOfAmmoSound:Play()
		Tool.Enabled = false
		task.wait(0.5)
		Tool.Parent = WeaponStorage
	end
end

Ammo:GetPropertyChangedSignal("Value"):Connect(checkAmmo)
Tool.Activated:Connect(checkAmmo)

you don’t need to use :GetPropertyChangedSignal "Value" on value objects because the .Changed event is changed to only fire when Value changes:

image
Sorry.

Do you even decrement Ammo.Value anywhere–or, is the value ever nonpositive?

There’s another script within the tool that decreases the Ammo value once its clicked.

The script stops working when its checking whether or not the Ammo.Value is less then or equal to zero.

This doesn’t work whatso ever… Seems like it doesn’t detect if the Ammo changed

1 Like

If your tool does not have a handle, set the tool’s property RequiresHandle to false.

There are many ways to do this. You have a few examples. Maybe something else isn’t set up correctly. I’m not testing here; I have nothing to work with, so it’s all really speculation.

If you want I could provide you with the model so you could have a better understanding of how my gun works

It turns out that the script that was decreasing the Ammo value was a local script… I changed it to a regular script and it fixed the problem.

show me more of the script and I will adjust it fully show me the part where the tool is “functioning” like shooting a bullet or damaging for ex i can clean it up too

also it just stops entirely right when it reaches the check?

Sure I’d take a look if you like. I have a few of them like that.