The title says it all. this gun has been causing me so many issues I just want something fixed.
heres the code.
Also there is a second bug where it shoots super slow the first time you shoot it but after that it shoots normally if you know why that happens please help me there to.
--- local script
local plr = game:GetService("Players").LocalPlayer
local anims = {script:WaitForChild("Hold"), script.Parent.Parent.Click}
local loadedAnims = {}
local tool = script.Parent
local animator
local debounce = true
local del = 0.1 --The cooldown between each click
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
shooting = false
script.Parent.Parent.Activated:Connect(function()
mouse.Button1Down:Connect(function()
shooting = true
if shooting == true then
repeat
wait()
if script.Parent.Parent.AmmoCounter.Value >=1 then
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local rayOrigin = script.Parent.Position
local raycastResult = workspace:Raycast(rayOrigin, mouse.UnitRay.Direction* 250 , raycastParams)
script.Parent.Parent.FireGun:FireServer(mouse.Hit.Position)
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local RecoilTime = 0.1
local RecoilHeight = 1.1
local NegativeRecoilTime = 0.05
local DistanceShake = 0
local SpeedShake = 0
local IncrementTime = 0.25 --0 to 1
local Connection, TwoConnection
local function PlayRecoil()
local TotalSpeed = SpeedShake * tick()
local bobbleX = math.cos(TotalSpeed * DistanceShake)
local bobbleY = math.abs(math.sin(TotalSpeed)) * DistanceShake
Camera.CFrame = Camera.CFrame:Lerp(Camera.CFrame * CFrame.new(0,bobbleY,0) * CFrame.Angles(math.rad(RecoilHeight),0,0), IncrementTime)
end
local function RecoverRecoil()
Camera.CFrame = Camera.CFrame:Lerp(Camera.CFrame * CFrame.Angles(math.rad(-NegativeRecoilTime),0,0), IncrementTime)
end
Connection = RunService.RenderStepped:Connect(PlayRecoil)
wait()
Connection:Disconnect()
wait()
TwoConnection = RunService.RenderStepped:Connect(RecoverRecoil)
wait(0.01)
TwoConnection:Disconnect()
end
if script.Parent.Parent.AmmoCounter.Value >=1 then
if(debounce) then --Setting up the debounce (cooldown)
debounce = false
wait()
animator = plr.Character:WaitForChild("Humanoid"):WaitForChild("Animator") --Find the animator object
if(not loadedAnims[2]) then --If the animation didn't exist yet
loadedAnims[2] = animator:LoadAnimation(anims[2]) --Load it
end
loadedAnims[2]:Play() --Play it
wait(del) --Wait the cooldown
debounce = true
end
end
until shooting == false
end
end)
end)
mouse.Button1Up:Connect(function()
repeat
shooting = false
until shooting == false
end)