Weapon Shot Issue

Hello, I’m working on a weapon, but I have a problem, I’m using two values to set Ammo count and number of Shot x click, but every time Unequip and then Equip the Item, the shot number become 1 more high, can someone help me ?

local Tool = script.Parent

local Ammo = script.Parent:WaitForChild("Ammo", 1)
local Shot = script.Parent:WaitForChild("Shot", 1)
local Value = script.Parent:WaitForChild("Value")
local reloading = false
local plr = game.Players.LocalPlayer
local GUI = script.Parent:WaitForChild("AmmoGUI", 1)

local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")


Tool.Equipped:Connect(function(Mouse)
	
	Ammo.Value = 10
	Shot.Value = 1
	Value.Value = true
	reloading = false
	
    GUI:Clone().Parent = plr.PlayerGui	
	wait()
	local text = plr.PlayerGui:FindFirstChild("AmmoGUI"):FindFirstChild("Frame"):FindFirstChild("Ammo")
	text.Text = (Ammo.Value).." / "..10
	
	local function reload()
		reloading = true
		wait(1)
		script.Parent.Reload:Play() -- change reloadingsound to what ever your reload sound is named
		Ammo.Value = 10
		Shot.Value = 1
		reloading = false
	end
	
	
	
	UIS.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 and Value.Value == true then
			if Ammo.Value > 0 and not reloading then
				Ammo.Value = Ammo.Value - Shot.Value
				script.Parent.ShotSound:Play() -- change gunshot to what ever your gun shot sound is named

			elseif reloading == false then
				reload()
				script.Parent.ShotSound:Stop()-- change gunshot to what ever your gun shot sound is named
			end

			while wait() do
				text.Text = (Ammo.Value).." / "..10
			end

		end
		
	end)


	UIS.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo.Value ~= 10 and Value.Value == true then
			reload()
		end
	end)

end)

Tool.Unequipped:Connect(function()
	if plr.PlayerGui:FindFirstChild("AmmoGUI") then
		plr.PlayerGui:FindFirstChild("AmmoGUI"):Destroy()
	end
	Value.Value = false
	reloading = false
end)

it may be because you are setting the values to their default as soon as the tool is equipped

Try moving that around, or remove it.
I may be reading this wrong though, so take what I said with a grain of salt

no it doens’t work anyway , it double the value every time I equip and unequip the tool

Just subtract one from the value if it is one higher, seems the simplest option to me.

nothing to do… the script do it anyway

Uhh, what you just said doesn’t make very much sense… You say the script is making the ammo count one higher than it was before, but you’re saying the script already subtracts that one as soon as it’s added?

The script runs, but I don’t know why every time I unequip and equip again the tool, the script counts the shot as double, so it substracts 2 and not 1

Then add one after subtracting two.

yes but if I equip the tool the first time and I don’t unequip it, it runs correctly

Yes, I understand this, but the simplest fix to your problem would be adding one to the ammo count after it subtracts two.

ok I fxed, the problem was a variable and a function thx anyway

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