I'm currently attempting to make a gun, and it works well outside of one error; whenever it is unequipped and re-equipped, it uses 1 more ammo than it should per shot. For example; initially, it uses 1 ammo per shot; however, after is unequipped and re-equipped, it uses 2 ammo per shot; and after it is unequipped and re-equipped once more, it uses 3 ammo per shot, and so on. I'm currently using a NumberValue to determine the amount of ammo used per shot, and a RemoteEvent and a server-side script to register damage. This is the section of code that governs the amount of ammo subtracted per shot:
script.Parent.Activated:Connect(function()
if Ammo.Value>0 and not Reloading then
Ammo.Value = Ammo.Value-1
sound:play()
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.Damage:FireServer(Mouse.Target.Parent, 20)
If needed, here is the entire client-side script:
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local Values = tool:WaitForChild("Values")
local Ammo = Values:WaitForChild("Ammo")
local MaxAmmo = 10
local mouse = player:GetMouse()
local Reloading = false
local UserInputService = game:GetService("UserInputService")
local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
reloadsound = Instance.new("Sound", workspace)
reloadsound.SoundId = "rbxassetid://269172810"
sound = Instance.new("Sound", workspace)
sound.SoundId = "rbxassetid://162043120"
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Icon = "rbxassetid://3324802476"
local function Reload()
Reloading = true
reloadsound:play()
Mouse.Icon = "rbxassetid://3324802476"
wait(2)
Ammo.Value = MaxAmmo
Mouse.Icon = "rbxassetid://3324802476"
Reloading = false
end
script.Parent.Activated:Connect(function()
if Ammo.Value>0 and not Reloading then
Ammo.Value = Ammo.Value-1
sound:play()
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.Damage:FireServer(Mouse.Target.Parent, 20)
end
elseif Reloading == false then
Reload()
end
print(Ammo)
end)
local Input = game:GetService("UserInputService")
Input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and Reloading == false and Ammo~= MaxAmmo then
reloadsound:play()
Reload()
end
end)
end)
Is there any way to make the ammunition subtracted not increase itself every time the firearm is unequipped and re-equipped?
