How can I make a hold to shoot system?

Yo,

I was wondering how could I make a hold to shoot system. So it shoots the gun as long as you are holding the click button.

Can anyone help?

Is this for mobile or PC?


1 Like

PC

character limit uzbuGBtgirguighizuhz

Maybe these could help -

1 Like

You can use UIS ( User Input Service ) to check if a player is holding down a key, and then make it process something.

Use UserInputService 's InputBegan and InputEnded events and set a boolean variable to either true or false.
In addition, you can use ContexActionService .

local Tool = script.Parent

local Toggle

local function OnActivated()
	Toggle = false
	while true do
		if Toggle then break end
		print("Hello world!")
		task.wait(1) --Gun's fire delay.
	end
end

local function OnDeactivated()
	Toggle = true
end

Tool.Activated:Connect(OnActivated)
Tool.Deactivated:Connect(OnDeactivated)

You can utilize a tool’s activated & deactivated events to achieve this.

1 Like

Remember to only use loops in localscripts as serversided scripts can cause slight performance issues

you can do like

local firing = false

while (firing) do
   -- put your shooting code here
end

This just isn’t true, a for/while/repeat construct is far more performant than a RemoteEvent/RemoteFunction.

2 Likes

Ah sorry for the misleading. Someone told me that and I just assumed it was true because of the nature of loops

1 Like

I managed to make it work on PC but when I use the same gun on mobile I have to continuously tap instead of just tap and hold.

1 Like