Ammo only subtracting once?

Incredibly strange, ammo is only subtracting once. It was working fine 2 days ago as well? Anyways, that’s all I have to say about the issue, here’s the server script:

local rt = game:GetService("ReplicatedStorage"):WaitForChild("Shoot")
local reloadrt = game.ReplicatedStorage.Reload
local debris = game:GetService("Debris")
local gunshot = game:GetService("SoundService"):WaitForChild("Gun Shot")

		local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://13112812781"
local reload = game.SoundService["Pistol Reload Sound"]
rt.OnServerEvent:Connect(function(plr, mouse, tool)
	local bullets = plr.Inventory.Bullet
	local char = plr.Character
	local hum = char.Humanoid
	local animator = hum.Animator
	local effect = tool.Effect.MuzzleEffect
	if tool.Name == "Pistol" then
		local damage = 10

		local debounce = false
		local ammo = script.Ammo.Value
		local ammo_out = false
		local firerate = 0.5
		local debounce2 = false
		if debounce == false and ammo_out == false and bullets.Value > 0 then
			reloadrt.OnServerEvent:Connect(function(plr)
				
	
				print("for")
				local reloadanim = Instance.new("Animation")
				reloadanim.AnimationId = "rbxassetid://13133924759"
				reloadanim.Parent = script
				local loadanim = char.Humanoid.Animator:LoadAnimation(reloadanim)
				reload:Play()
				ammo_out = true
				loadanim:Play()
				effect.Enabled = false
				task.wait(2)
				ammo = 16
				reload:Stop()
				reloadanim:Destroy()
				ammo_out = false		
			end)
			local loadanim = animator:LoadAnimation(animation)
			loadanim:Play()
			gunshot:Play()
			bullets.Value -=1
			print(mouse)
			local bullet = Instance.new("Part")
			bullet.Size = Vector3.new(1,1,1)
			bullet.Position = mouse
			bullet.Transparency = 1
			bullet.CanCollide = true
			bullet.Parent = workspace
			ammo -=1
			print(tostring(ammo))
			bullet.Touched:Connect(function(hit)
				print("hit")
				if hit.Parent:FindFirstChild("Humanoid") then
					warn("hum")
					if debounce2 == false then
						if hit.Parent.Name == plr.Character.Name then return end
						hit.Parent.Humanoid:TakeDamage(damage)
						print("wow")
						debounce2 = true
					end
				else
					warn("no hum")
				end
			end)

			debris:AddItem(bullet, firerate)
			effect.Enabled = true
			if ammo <= 0 then
				local reloadanim = Instance.new("Animation")
				reloadanim.AnimationId = "rbxassetid://13133924759"
				reloadanim.Parent = script
				local loadanim = char.Humanoid.Animator:LoadAnimation(reloadanim)
				reload:Play()
				print("reload")
				ammo_out = true
				loadanim:Play()
				effect.Enabled = false
				while true do
					ammo = 0
					task.wait(2)
					break
				end
					

				ammo += 2
				reload:Stop()
				reloadanim:Destroy()
				ammo_out = false
			end
			debounce = true

			print("debounce1")
			task.wait(firerate)
			debounce = false
			effect.Enabled = false
			print("debounce 2")

		end
	end
	

end)

Here is the local script:



local rt = game:GetService("ReplicatedStorage"):WaitForChild("Shoot")
local remaining = game.ReplicatedStorage.Remaining
print("inf")
local reloadrt = game.ReplicatedStorage.Reload
local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local plr = game:GetService("Players").LocalPlayer
local tool = script.Parent
local playergui = plr.PlayerGui
local debouncereload = false
local uis = game:GetService("UserInputService")
local debounce = false
local value = plr.Inventory.Bullet
local char = plr.CharacterAdded:Wait()
local ammo_out = false
local rt = game.ReplicatedStorage.Shoot
local ammo = 16
tool.Equipped:Connect(function()
	screengui1 = game.ReplicatedStorage.AmmoGui:Clone()
	screengui1.Parent = playergui
	screengui1.Enabled = true
	text = screengui1.Frame.Ammo
	text.Text = tostring(ammo) .. "/16"
	if value.Value <= ammo then
		text.Text = tostring(value.Value) .. "/16"

	end
	if value.Value <=0 then
		text.Text = "Out of Ammo"
		text.TextColor3 = Color3.new(1, 0, 0)
		value.Changed:Connect(function()
			text.Text = tostring(value.Value) .. "/16"
			text.TextColor3 = Color3.new(1, 1, 1)

		end)
	end
end)
tool.Unequipped:Connect(function()
	screengui1.Enabled = false
	print("em")
end)
tool.Activated:Connect(function()
	print("oh")
	print(ammo .. ": From the Client, ammo value")
	print(value.Value .. ": From the Client, bullet value")
		if debounce == false and ammo_out == false then
	
		if value.Value < ammo then
						text.TextColor3 = Color3.new(1, 1,1)
			print("aa")
			rt:FireServer(mouse.Hit.Position, script.Parent)
			text.Text = tostring(value.Value) .. "/16"
			print("baaaa")
		end
		if value.Value <=0 or ammo <= 0 then
			print("AHEM")
			text.Text = "Out of Ammo"
			print(text.Text)
			text.TextColor3 = Color3.new(1, 0, 0)
			return
		end
		if ammo < value.Value then
		text.Text = tostring(ammo) .. "/16"
		print("high and low")
			text.TextColor3 = Color3.new(1, 1,1)
			print("aa")
			rt:FireServer(mouse.Hit.Position, script.Parent)
			ammo -=1
			print("sent")
			end
		if ammo == 0 then

				ammo_out = true
			text.Text = "Reloading..."
print("ay")			
			task.wait(2)
			ammo = 16
			if value.Value < ammo then
				print("bee")
				text.Text = value.Value .. "/16"
				ammo_out = false
				return
			end
			print("B OO")
				ammo_out = false
				text.Text = "16/16"
			end
			debounce = true
			task.wait(0.5)
			debounce = false
	end
	end)

uis.InputBegan:Connect(function(input, istyping)
	if istyping then return end
	if input.KeyCode == Enum.KeyCode.R then
		if value.Value <=0 then return end
		if ammo == 16 then return end
		if debounce == false then
		reloadrt:FireServer()
		ammo_out = true
		text.Text = "Reloading..."
		task.wait(2)
		ammo_out = false
		text.Text = "16/16"
			ammo = 16
			debounce = true
			task.wait(1)
			debounce = false
			end
end
end)

Connecting remote events within a connection like this is a bad idea, as it will cause memory leaks. Also, hardcoding information about each type of gun is a great way to create an incredibly messy script. I suggest storing information inside a dictionary, modulescript, or configuration.

Thank you for letting me know about the memory leak issue.

I’ll see what I can do with a module script to configure the gun
Thanks a lot

Uhhh you set the ammo to be the ammo’s value meaning if i had a value of 2 in there subtracting 1 from the variable local ammo = game.ServerStorage.Ammo.Value then because it’s a saved value it would just go to 1 but not update so what you want to do is local ammo = game.ServerStorage.Ammo then subtract 1 from the value that way so ammo.Value -= 1

This did not work.
This time, the ammo just stayed on 16

Sorry messed up it’s meant to be ammo.Value -= 1

It wasn’t meant to be ammo.value -=1.

oh oops read it wrong. Thought you had saved it as a value

Does it not even print the right ammo value?

It prints the value, the value is just not reaching 0
I need it to reach 0 for the reload

Could it be your setting the value to 0 before an event fires. I noticed 2 if statements both checking if the ammo was 0 and eventually setting it back

They are meant to set it back as it is a reload mechanic

I know but there where 2 so one could fire like milliseconds before the other causing the other bit to not trigger a reload

There wasn’t two in the server script, the local script one does not interfere

but then in the server script ammo is a value. Which is what I said earlier about removing the .Value bit and actually doing that when you minus it

In the server script you define the ammo variable when the player shoots. You are defining it as script.Ammo.Value, which never actually changes. You then remove 1 from that variable without actually updating script.Ammo.Value and then you print out ammo. Ammo will always stay the same that way since it’s never updated.

that’s what i said lol but then they said no :frowning:

you’re right, they just don’t know how to fix it because the script is a mess. @exp_lol123 you should watch some tutorial videos on how to make a proper gun, also rewrite your code and use more variables so your code makes more sense to you and everyone else.

I was confused with the wording

1 Like