Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I connect my ContextActionService to my function while holding down button1?

Asked by
nanaluk01 247 Moderation Voter
6 years ago
Edited 6 years ago

I am trying to bind my ContextActionService to my function while holding down my left mouse-button. (Making the function continue running while I am holding Button1 down)

Currently I have this: (The coding is in a ModuleScript, which I use for other stuff - it has to be in a modulescript in order for my game to work)

local function inputPlace(_, userInputState, inputObject)
    if (userInputState == Enum.UserInputState.End) then
        print("Running while holding down Button1")
    end
end

local function bindInputs()
    if (not OVERRIDE_CONTROLS) then
        game:GetService("ContextActionService"):BindActionAtPriority("TryingThisNewStuff", inputPlace, false, 9 Enum.UserInputType.MouseButton1)
    end
end

Now, this only runs when I quit holding down my Button1.

I want the function to continue running while I am holding down the Button1, but I do not know how to make this happen. I have searched the wiki, and I can not find any solutions.

Any help is greatly appreciated!

1 answer

Log in to vote
0
Answered by 6 years ago

You want to execute something as long as they are holding the left click. To do this, you should have a variable that is true when they click it down, and false when they click it up. I'm not entirely sure what you wish to do, but here is the structure I would use:

local holding = false
local UserInputService = game:GetService("UserInputService")

local function started(input,gameProcessed) -- When they start
    if input.UserInputType == Enum.UserInputType.MouseButton1 and not holding and not gameProcessed then
        holding = true
        -- execute whatever you want to happen when they click it
        repeat wait() until not holding
        --execute whatever you want to happen when they stop holding
    end
end
local function ended(input,gameProcessed) -- When they let go
    if input.UserInputType == Enum.UserInputType.MouseButton1 and holding and not gameProcessed then
        holding = false -- they are no longer holding it
    end
end

--Connect the functions to events with UserInputService
UserInputService.InputBegan:Connect(started)
UserInputService.InputEnded:Connect(ended)

Please note I just made this, so reply to me if there are any errors! I hope I helped, and please accept the answer if I did!

Ad

Answer this question