So the problem with the gun is, it works fine the first time a player equips it, but once it's unequipped and reequipped, the gun shoots 2 bullets at once, and once it's unequipped and reequipped again, it will shoot 3 bullets, and so on. How do I stop this?
Here's my localscript inside the gun:
local maxAmmo = 12
local ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local textLabel = playerGui:WaitForChild("Ammo"):FindFirstChild("AmmoCount")
local InRound = game.ReplicatedStorage.InRound
local ammoCount = game.Players.LocalPlayer.PlayerGui.Ammo.AmmoCount
local cursorId = "http://www.roblox.com/asset/?id=117431027"
local mouse = game.Players.LocalPlayer:GetMouse()
local team = game:GetService("Teams")
local leftButtonDown
function onUnequipped()
leftButtonDown = false
reloading = false
ammoCount.Visible = false
mouse.Icon = ""
end
local function reload()
reloading = true
script.Parent.Reload:Play()
wait(3)
ammo = maxAmmo
reloading = false
end
local function onFire()
if leftButtonDown and ammo > 0 and not reloading and InRound.Value then
ammo = ammo - 1
script.Parent.GunShot:Play()
local name = mouse.Target.Parent.Name
if mouse.Target.Parent:FindFirstChild("Humanoid") and InRound.Value and game.Players.name.team ~= player.team then
print(player.team)
print(game.Players.name.team)
script.Parent.dealDmg:FireServer(mouse.Target.Parent, 18.5)
end
elseif not reloading then
reload()
script.Parent.GunShot:Stop()
end
while wait() do
textLabel.Text = (ammo).." / "..maxAmmo
end
end
local function onMouseDown()
leftButtonDown = true
onFire()
end
local function onMouseUp()
leftButtonDown = false
end
script.Parent.Equipped:Connect(function()
mouse.Icon = cursorId
ammoCount.Visible = true
mouse.Button1Down:Connect(onMouseDown)
mouse.Button1Up:Connect(onMouseUp)
local input = game:GetService("UserInputService")
input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and not reloading and ammo ~= maxAmmo then
reload()
end
end)
end)
script.Parent.Unequipped:Connect(onUnequipped)
And here's my server script inside the gun:
function tagHumanoid(humanoid, player)
local creator = Instance.new("ObjectValue")
creator.Value = player
creator.Name = "creator"
creator.Parent = humanoid
end
function untagHumanoid(humanoid)
if humanoid ~= nil then
local tag = humanoid:FindFirstChild("creator")
if tag ~= nil then
tag.Parent = nil
end
end
end
script.Parent.dealDmg.OnServerEvent:Connect(function(player, Target, Damage)
local humanoid = Target.Humanoid
tagHumanoid(humanoid, player)
humanoid:TakeDamage(Damage)
wait(1)
untagHumanoid(humanoid)
end)
