Help with script

i am making a gun that has console support the reload keybind for console is buttonL1 and shoot is buttonR2
whenever i move when the script has console support the gun stops shooting like on the video
bad.wmv (1.6 MB)

without console support:

good.wmv (2.2 MB)

this is the script

--!nocheck
-- SERVICES --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

-- VARIABLES --
local player = Players.LocalPlayer
local gun = script.Parent
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
mouse.TargetFilter = workspace:WaitForChild("FilteringFolder")
local origMouseIcon = "rbxasset://SystemCursors/Arrow"
local properties = require(script.Parent:WaitForChild("Properties"))

-- Animations
local character = workspace:WaitForChild(player.Name) -- stops animationclipprovider service error
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator") :: Animator

local animInstanceEquip = Instance.new("Animation")
local animInstanceShoot = Instance.new("Animation")
local animInstanceReload = Instance.new("Animation")
animInstanceEquip.AnimationId = properties.Animations.AnimEquip
local equipTrack = animator:LoadAnimation(animInstanceEquip)
animInstanceShoot.AnimationId = properties.Animations.AnimShoot
local shootTrack = animator:LoadAnimation(animInstanceShoot)
animInstanceReload.AnimationId = properties.Animations.AnimReload
local reloadTrack = animator:LoadAnimation(animInstanceReload)

local gui = script.Parent:WaitForChild("GunGUI")

local MobileButtonsFrame = gui:WaitForChild("MobileButtons")

-- params
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude

-- for events
local UISConnection1 = nil
local UISConnection2 = nil

-- event
local gunEvent = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("GunEvent")

-- debounces
local shotCooldown = 60/properties.FireRate -- max theoretical fire rate is 3600
local mouseButtonDown = false
local reloading = false
local shooting = false

-- FUNCTIONS --
local function updateText(element, text)
	element.Text = text
end
updateText(gui.Frame.MagAmmoText, "MAG AMMO: "..gun:GetAttribute("AmmoInMag"))
updateText(gui.Frame.ReserveAmmoText, "RESERVE AMMO: "..gun:GetAttribute("AmmoReserve"))
-- text in GUI are blank by default, so we must set them in script

local function createSound(parent, soundProperties)
	local sound = Instance.new("Sound")
	for property,value in pairs(soundProperties) do
		sound[property] = value
	end
	sound.Parent = parent
	return sound
end

local function createHitmarker(result)
	if not result then return end
	if result.Instance.Parent:FindFirstChildWhichIsA("Humanoid") and not Players:GetPlayerFromCharacter(result.Instance.Parent) then
		local sound = createSound(game:GetService("SoundService"), properties.HitmarkerSoundProperties)
		sound:Play()
		sound.Ended:Connect(function()
			sound:Destroy()
		end)
		gui.Hitmarker.Position = UDim2.new(0,mouse.X,0,mouse.Y)
		if result.Instance.Name == "Head" then
			gui.Hitmarker.ImageColor3 = Color3.fromRGB(255, 137, 137)
		end
		task.spawn(function()
			gui.Hitmarker.Visible = true
			task.wait(0.08)
			gui.Hitmarker.Visible = false
			gui.Hitmarker.ImageColor3 = Color3.fromRGB(255, 255, 255)
		end)
	end
end

local function reload()
	if gun:GetAttribute("AmmoInMag") == properties.MaxMagAmmo or gun:GetAttribute("AmmoReserve") == 0 then return end
	if reloading then return end
	reloading = true
	updateText(gui.Frame.MagAmmoText, "RELOADING")
	reloadTrack:Play()
	ReplicatedStorage.Remotes.ReloadAnim:Fire(true, "AK47")
	reloadTrack:AdjustSpeed(1/properties.ReloadDuration)
	reloadTrack.Ended:Wait()
	if reloading then
		gunEvent:FireServer("ValidateReload")
		reloading = false
	else
		ReplicatedStorage.Remotes.ReloadAnim:Fire(false, "AK47")
		updateText(gui.Frame.MagAmmoText, "MAG AMMO: "..gun:GetAttribute("AmmoInMag"))
		-- incase player unequips gun during reload animation
	end
end

local function shoot()
	params.FilterDescendantsInstances = {player.Character, workspace.FilteringFolder}
	local shotDirection = (mouse.Hit.Position - gun.Handle.Muzzle.WorldCFrame.Position).Unit

	if gun:GetAttribute("GunType") == "Standard" then
		local result = workspace:Raycast(gun.Handle.Muzzle.WorldCFrame.Position, shotDirection*properties.MaxBulletDistance, params)
		if result then
			gunEvent:FireServer("ValidateShot", result.Instance, result.Position, result.Normal, result.Instance.Position)
		else
			gunEvent:FireServer("ValidateShot")
		end
		createHitmarker(result)
	elseif gun:GetAttribute("GunType") == "Spread" then
		local rng = Random.new()
		local allPellets = {}
		for i = 1, properties.TotalPellets do
			if properties.RoundType ~= "Slug" then
				shotDirection = (CFrame.lookAt(gun.Handle.Muzzle.WorldCFrame.Position, mouse.Hit.Position) *
					CFrame.Angles(math.rad(rng:NextNumber(-properties.MaxSpread,properties.MaxSpread)),
						math.rad(rng:NextNumber(-properties.MaxSpread,properties.MaxSpread)),0)).LookVector
			end
			-- gets the direction from the starting point to the mouse point with a little bit of randomness in the angles
			local result = workspace:Raycast(gun.Handle.Muzzle.WorldCFrame.Position, shotDirection*properties.MaxBulletDistance, params)
			if result then
				result = {["Instance"] = result.Instance, ["Position"] = result.Position, ["Normal"] = result.Normal, ["InstanceOriginPosition"] = result.Instance.Position}
				table.insert(allPellets, result)
				-- storing all results and then sending to server allows us to only fire event once
			end
		end
		gunEvent:FireServer("ValidateShot", allPellets)
		local bodyShots = {}
		for _,result in ipairs(allPellets) do
			-- create a hit marker for only ONE of the pellets
			if result.Instance.Name == "Head" then
				-- if any pellet hit a headshot
				createHitmarker(result)
				bodyShots = {}
				break
			elseif result.Instance.Parent:FindFirstChildWhichIsA("Humanoid") then
				table.insert(bodyShots, result)
			end
		end
		if #bodyShots > 0 then
			createHitmarker(bodyShots[1])
		end
	end
	ReplicatedStorage.Remotes.IsShooting:Fire(true, "AK47")
	shootTrack:Play()
end

-- Event handlers --
gun.Equipped:Connect(function()
	ReplicatedStorage.Remotes.Idle:Fire(true, "AK47")
	equipTrack:Play()
	gui.Parent = player.PlayerGui
	mouse.Icon = "rbxassetid://11839767092"
	if UIS.TouchEnabled then
		MobileButtonsFrame.Visible = true
	else
		MobileButtonsFrame.Visible = false
	end

	UISConnection1 = UIS.InputBegan:Connect(function(input, gameProcessed)
		if gameProcessed then return end
		if mouseButtonDown then return end
		
		MobileButtonsFrame:WaitForChild("Reload").MouseButton1Down:Connect(function()
			reload()
		end)

		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch or input.KeyCode == Enum.KeyCode.ButtonR2 then
			print(shooting)
			print(reloading)
			if shooting then return end
			if reloading then return end
			print(shooting)
			print(reloading)
			if gun:GetAttribute("AmmoInMag") == 0 then 
				-- play 'clink' when no ammo
				local sound = createSound(gun.Handle, properties.GunClickSoundProperties)
				sound:Play()
				sound.Ended:Wait()
				sound:Destroy()
				return
			end
			shooting = true
			if properties.Automatic then
				mouseButtonDown = true
				while mouseButtonDown do
					shoot()
					task.wait(shotCooldown)
					if gun:GetAttribute("AmmoInMag") <= 0 then mouseButtonDown = false end
				end
			else
				shoot()
				task.wait(shotCooldown)
			end
			shooting = false
		elseif input.KeyCode == Enum.KeyCode.R or input.KeyCode == Enum.KeyCode.ButtonL1 then
			reload()
		end
	end)

	UISConnection2 = UIS.InputEnded:Connect(function(input, gameProccessed)
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch or Enum.KeyCode.ButtonR2 then
			mouseButtonDown = false
		end
	end)
end)

gun.Unequipped:Connect(function()
	mouse.Icon = origMouseIcon
	gui.Parent = gun
	equipTrack:Stop()
	shootTrack:Stop()
	reloadTrack:Stop()
	ReplicatedStorage.Remotes.IsShooting:Fire(false, "AK47")
	ReplicatedStorage.Remotes.ReloadAnim:Fire(false, "AK47")
	ReplicatedStorage.Remotes.Idle:Fire(false, "AK47")

	mouseButtonDown = false
	shooting = false
	reloading = false
	UISConnection1:Disconnect()
	UISConnection2:Disconnect()
end)

gun.AttributeChanged:Connect(function(name)
	if name == "AmmoInMag" then
		updateText(gui.Frame.MagAmmoText, "MAG AMMO: "..gun:GetAttribute("AmmoInMag"))
	elseif name == "AmmoReserve" then
		updateText(gui.Frame.ReserveAmmoText, "RESERVE AMMO: "..gun:GetAttribute("AmmoReserve"))
	end
end)

if gun:GetAttribute("GunType") == "Spread" then
	shootTrack:GetMarkerReachedSignal("Pump"):Connect(function()
		-- for shotguns only, play the pump sound when pump event is reached
		local sound = createSound(gun.Handle.Muzzle, properties.ShotgunPumpSoundProperties)
		sound:Play()
		sound.Ended:Wait()
		sound:Destroy()
	end)
end

-- for updating properties when told by server
gunEvent.OnClientEvent:Connect(function(action, passedGun, property, value)
	if passedGun ~= gun then return end

	if action == "UpdateProperty" then
		properties[property] = value
	end
end)
1 Like