Ok so when I fire my gun it fires at the right FireRate, but when I stop firing then fire again it goes faster. If I do it enough times it ends up shooting 18 bullets in 0.3 seconds.
-----------Settings Variables----------
local FireRate = 0.5
local Damage = math.random(30,70)
local ReloadTimes = 2
---------------------------------------
local Player = game.Players.LocalPlayer
local mouse_hold = true
local Character = Player.Character or Player.CharacterAdded:wait()
local Camera = workspace.CurrentCamera
local tool = script.Parent
local action_fire = false
local player = game:GetService("Players").LocalPlayer
local Ammo = script.Parent:WaitForChild("Ammo")
tool.Equipped:connect(function(mouse)
print("Tool equipped!")
holdanim = player.Character.Humanoid:LoadAnimation(script.Parent.HoldAnim)
reloadanim = player.Character.Humanoid:LoadAnimation(script.Parent.ReloadAnim)
mouse.Button1Down:connect(function(startfire)
if Ammo.Value >= 1 and action_fire == false then
action_fire = true
while true do
wait(FireRate)
if Ammo.Value >= 1 and action_fire == true then
print("Mouse pressed!")
Ammo.Value = Ammo.Value - 1
print(Ammo.Value)
script.Parent.Handle.Fire:Play()
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Gold")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.99
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Main.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.1)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(Damage)
end
end
end
end
end
wait(FireRate)
end)
end)
local Player = game.Players.LocalPlayer
local action_reload = false
Mouse = Player:GetMouse()
Mouse.KeyDown:connect(function(Key)
if(Key:lower() == "r") and Ammo.Value < 18 and action_reload == false then
action_reload = true
script.Parent.Handle.Reload:Play()
reloadanim.Looped = false
reloadanim:Play()
wait(ReloadTimes)
Ammo.Value = 18
print(Ammo.Value)
action_reload = false
end
end)
script.Parent.Equipped:connect(function(Mouse)
Mouse.Button1Up:connect(function(stopfire)
action_fire = false
print("Stopped Firing")
wait(FireRate)
end)
end)
