How do you detect if ButtonR2 is held down or not with ContextActionService?

  1. What do you want to achieve? Keep it simple and clear!
    I wanna achieve a gun with automatic fire with console. Its already compatible with pc.
  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to detect if ButtonR2 is held or not.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I haven’t tried anything yet because I have just learned about ContextActionService

Here is my code for the client, server is not needed (i think.)

local ReplicatedStorage = game.ReplicatedStorage
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local Debris = game:GetService("Debris")
local ContextActionService = game:GetService("ContextActionService")
local BindForShoot = Enum.KeyCode.ButtonR2
local BindForReload = Enum.KeyCode.ButtonX
--
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local character = Player.Character or Player.CharacterAdded:Wait()
print(character.Name)
local PlayerGui = Player:WaitForChild("PlayerGui")
local GunGui = PlayerGui:WaitForChild("GunGui")
local AmmoAmountLabel = GunGui:WaitForChild("Amount")
local MuzzleFlash = script.Parent.Parent:WaitForChild("BulletHole").MuzzleFlash
--
local Sounds = script.Parent:WaitForChild("Sounds")
local Values = script.Parent:WaitForChild("Values")
--
local ReloadTime = Values:WaitForChild("ReloadTime")
local Damage = Values:WaitForChild("Damage")
local HeadshotMulti = Values:WaitForChild("HeadshotMulti")
local CurrentAmmo = Values:WaitForChild("CurrentAmmo")
local ReserveAmmo = Values:WaitForChild("ReserveAmmo")
local MaxAmmo = Values:WaitForChild("MaxAmmo")
--
local ReloadSound = Sounds:WaitForChild("Reload")
local ShootSound = Sounds:WaitForChild("Shoot")
local HeadshotSound = Sounds:WaitForChild("Headshot")
--
local ReloadAnim = script.Parent:WaitForChild("Animations").Reload
local RecoilAnim = script.Parent:WaitForChild("Animations").Recoil
local HoldAnimation = script.Parent:WaitForChild("Animations").Hold
--
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local RemoteFunctions = ReplicatedStorage:WaitForChild("RemoteFunctions")
--
local ReloadFunction = RemoteFunctions:WaitForChild("Reload")
local ShootEvent = RemoteEvents:WaitForChild("Shoot")
local CanShootFunction = RemoteFunctions:WaitForChild("CanShoot")
local ReloadEvent = RemoteEvents:WaitForChild("Reload")
--
local mouseDown = false
local shooting = false
local equipped = false
local reloading = false
local BulletHole = script.Parent.Parent:WaitForChild("BulletHole")
local db = false
local hold

--Functions--

local function fire()
	local result = CanShootFunction:InvokeServer(CurrentAmmo)
	if not reloading then
		if result == true then
			character:WaitForChild("Humanoid"):LoadAnimation(RecoilAnim):Play()
			shooting = true
			local pos1 = BulletHole.Position
			local pos2 = Mouse.Hit.p
		--
			local ray = Ray.new(pos1,(pos2-pos1).Unit * 100)
			local touched, direction = workspace:FindPartOnRay(ray,Player.Character,false,true)
			local Dist = (pos1-pos2).magnitude
			local Lazer = Instance.new("Part")
	 		Lazer.Parent = game.Workspace
			Lazer.Anchored = true
	 		Lazer.CanCollide = false
	 		Lazer.Size = Vector3.new(0.05,0.05,Dist)
			Lazer.CFrame = CFrame.new(pos1,direction)*CFrame.new(0,0,-Dist/2)
			Lazer.Material = Enum.Material.Neon
			Lazer.BrickColor = BrickColor.new("Toothpaste")
			MuzzleFlash.Enabled = true
			wait(0.006)
			MuzzleFlash.Enabled = false
			
			Debris:AddItem(Lazer,0.1)
			if touched then
				ShootEvent:FireServer(touched,Damage,ShootSound,CurrentAmmo)
			end
		end
	end
end
local function ActivatedTool()
	mouseDown = true
	if not db then
		if reloading == false then
			db = true
			shooting = true
			repeat fire() print("Shooting")	
				wait(0.07)
			until not mouseDown 
			wait(.08)
			shooting = false
			db = false
		end
	end
end
Mouse.Button1Up:Connect(function()
	mouseDown = false
end)
script.Parent.Parent.Parent.Equipped:Connect(function()
	equipped = true
	game.ReplicatedStorage.RemoteEvents.ConnectM6D:FireServer(script.Parent)
	
	character.Torso.ToolGrip.Part0 = character.Torso
	character.Torso.ToolGrip.Part1 = script.Parent
	hold = character:WaitForChild("Humanoid"):LoadAnimation(HoldAnimation)
	hold:Play()
end)
script.Parent.Parent.Parent.Unequipped:Connect(function()
	equipped = false
	game.ReplicatedStorage.RemoteEvents.DisconnectM6D:FireServer()
	hold:Stop()
	
end)
UserInputService.InputBegan:Connect(function(input)	
	if equipped then
		if input.KeyCode == Enum.KeyCode.R then
			if shooting == false then
				local result = ReloadFunction:InvokeServer(CurrentAmmo,ReserveAmmo,ReloadSound,ReloadTime.Value,MaxAmmo.Value,ReloadAnim)
				if result == true then
					ReloadEvent:FireServer(CurrentAmmo,ReserveAmmo,ReloadSound,ReloadTime.Value,MaxAmmo.Value,ReloadAnim)
					AmmoAmountLabel.Text = CurrentAmmo.Value.."/"..ReserveAmmo.Value
				end
			end
		end
	end
end)
CurrentAmmo.Changed:Connect(function()
	AmmoAmountLabel.Text = CurrentAmmo.Value.."/"..ReserveAmmo.Value
end)
-- use functions
script.Parent.Parent.Parent.Activated:Connect(ActivatedTool)

More to know:
I used a gun tutorial for making gun parts animatable so the parent of the script is a part, parent of that is a model, and then the parent of that is the tool.
Also I’m gonna change all UserInputservice functions to ContextActionService ones.

Thanks for reading

1 Like

can you explain it a bit more? if you want the button to be held down, make a bool that turns true when on inputbegan when the button is held and false in inputended when its released

I’ll try that but right now you are talking about UserInputService which isn’t what I was talking about.

you can make a bool when the button is held down, and while it’s held down, fire the “fire()” function

something like:

repeat
    wait()
    fire()
until not buttonDown

or you can use CAS, which also works like UIS but with more button inputs, a mobile button etc

2 Likes

I use UserInputService and it works fine and it’s easy to set up. Bascially keydown set variable to true > vice versa for key up. Then use Runservice to check is the variable true. If so, run the function or code.

1 Like