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

How do you use MouseButton down?

Asked by 8 years ago

Is it possible to to have two different functions that start when you use mousebutton1down? at the same time.

0
You can... connect to different functions after the first function has been fired. By naming a function then calling it with "functionName()". User#11440 120 — 8y

3 answers

Log in to vote
0
Answered by
Wutras 294 Moderation Voter
8 years ago

What you mean is most likely going to be coroutines. Coroutines are a possibility to run functions seperately from the script. You use them like this:

local cor=coroutine.create(function()
    print("This is a coroutine!")
end)
coroutine.resume(cor)
-- Or
function ForCor()
    print("This will be a coroutine")
end
coroutine.resume(ForCor)

A coroutine requires a lua function! You can't use coroutine.create() on other coroutines or other stuff than lua functions! Keep that in mind. There are also other methods for coroutine. Like coroutine.yield(), which is used to STOP(Not end) running coroutines. There also is coroutine.running, which can help you to find out whether a coroutine is running or not. coroutine.status() will tell you the status of a coroutine(stopped, running, etc.). And the last one is coroutine.wrap(), which creates a new coroutine too. If you want to connect it with the MouseButton1Down event then it would look like this:

button=script.Parent.Button
button.MouseButton1Down:connect(function()
    local cor=coroutine.create(function()
        print("This is the first function!")
    end)
    local cor2=coroutine.create(function()
        print("This is the second function!")
    end)
    coroutine.resume(cor)
    coroutine.resume(cor2)
end)

I hope this helps you. If you have any other questions, feel free to ask!

Ad
Log in to vote
0
Answered by
Edenojack 171
8 years ago

There are two methods to this, one will work with lots of calls from different scripts, the other will disable the other things.

First, User Input:

local UserInputService = game:GetService('UserInputService')
UserInputService.InputBegan:connect(function(mousecheck)
    if mousecheck.UserInputType == Enum.UserInputType.MouseButton1 then
        --Start your function
    end 
end)
UserInputService.InputEnded:connect(function(mousecheck)
    if mousecheck.UserInputType == Enum.UserInputType.MouseButton1 then
        --Stop your function.
    end 
end)

This can be called from loads of scripts, with no problem. However, if you want to have some functions that are disabled while others are open, you can use the Context Action Service. This is typically used for Gamepad/Mobile games, but has a fairly wide application.

game.ContextActionService:BindAction("Cool Name", FUNCTIONNAME, false, Enum.UserInputType.MouseButton1)

This will means that now, every time you click the mouse button, it will call FUNCTIONNAME. Which is super handy, and super easy.

Log in to vote
0
Answered by 8 years ago
--If this helped you understand please make sure to click Accept Answer

Menu = script.Parent.Parent.Menu --Variable used for the function to script much faster and easier [MENU]
Button = script.Parent --Variable used for the function to script much faster and easier [Button]

-- This is a GUI Opening Script
--Menu and Button are separate in a GUI

function open()
    if Menu == true then  -- What happens when you click goes inside open
        Menu.Visible = false -- What happens when you click goes inside open
end


Button.MouseButton1Down(open)

--This here ^^ calls the function and states that you must click Button
--If you dont have a variable for 'Button' then it would just be this 
--vvvvv

script.Parent.MouseButton1Down(open)

Answer this question