Trying to make a gun system and when you shoot it subtracts one ammo
Every time you reselect it subtracts one more ammo
*equip subtracts 1, deselect and equip again subtracts 2, so on and so forth.
local maxAmmo = 24
local ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local textLabel = PlayerGui:WaitForChild("GunUI"):WaitForChild("AmmoText")
local gunui = PlayerGui:WaitForChild("GunUI")
local mouseui = game.Players.LocalPlayer:GetMouse()
-- 6704039473
-- 46537754
script.Parent.Equipped:Connect(function(mouse)
mouse.Icon = 'rbxassetid://6231821602'
gunui.Enabled = true
local function reload()
reloading = true
script.Parent.Reload:Play()
wait(1)
ammo = maxAmmo
reloading = false
end
script.Parent.Activated:Connect(function()
if ammo > 0 and not reloading then
ammo = ammo - 1
script.Parent.Barrel.Smoke.Enabled = true
script.Parent.Fire:FireServer()
wait(0.1)
script.Parent.Barrel.Smoke.Enabled = false
if mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.Damage:FireServer(mouse.Target.Parent, 20)
end
elseif reloading == false then
reload()
script.Parent.GunShot:Stop()
end
while wait() do
textLabel.Text = (ammo).."/"..maxAmmo
end
end)
end)
Actually, that’s completely useless. Disconnecting doesn’t do anything, the event will still fire regardless.
(Unless you only wanted it to fire one time, in which you would use :Wait()).
No idea if I did this right but it’s doing the same thing
local equipConn = nil
conn = script.Parent.Equipped:Connect(function(mouse)
mouse.Icon = 'rbxassetid://6231821602'
gunui.Enabled = true
local function reload()
reloading = true
script.Parent.Reload:Play()
wait(1)
ammo = maxAmmo
reloading = false
end
script.Parent.Unequipped:Connect(function()
equipConn:Disconnect()
end)
script.Parent.Activated:Connect(function(shoot)
if ammo > 0 and not reloading then
ammo = ammo - 1
script.Parent.Barrel.Smoke.Enabled = true
script.Parent.Fire:FireServer()
wait(0.1)
script.Parent.Barrel.Smoke.Enabled = false
if mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.Damage:FireServer(mouse.Target.Parent, 20)
end
elseif reloading == false then
reload()
script.Parent.GunShot:Stop()
end
while wait() do
textLabel.Text = (ammo).."/"..maxAmmo
end
end)
local Input = game:GetService("UserInputService")
Input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and reloading == false then
reload()
end
end)
end)
local Input = game:GetService("UserInputService")
local equipConn = nil
local reloadConn = nil
local function reload()
reloading = true
script.Parent.Reload:Play()
wait(1)
ammo = maxAmmo
reloading = false
end
script.Parent.Equipped:Connect(function(mouse)
mouse.Icon = 'rbxassetid://6231821602'
gunui.Enabled = true
equipConn = script.Parent.Activated:Connect(function(shoot)
if ammo > 0 and not reloading then
ammo -= 1
script.Parent.Barrel.Smoke.Enabled = true
script.Parent.Fire:FireServer()
wait(0.1)
script.Parent.Barrel.Smoke.Enabled = false
if mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.Damage:FireServer(mouse.Target.Parent, 20)
end
textLabel.Text = (ammo).."/"..maxAmmo
elseif not reloading then
reload()
script.Parent.GunShot:Stop()
end
end)
reloadConn = Input.InputBegan:Connect(function(Key, gpe)
if Key.KeyCode == Enum.KeyCode.R and not reloading and not gpe then
reload()
end
end)
end)
script.Parent.Unequipped:Connect(function()
equipConn:Disconnect()
reloadConn:Disconnect()
end)
You don’t need all those events in the equipped function, the unequipped should be outside