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

Should I be using UserInputService instead of MouseButton1Down?

Asked by 7 years ago

Title says it all really

I'm just wondering, because I want to add console support later on and I know mousebutton1down wont work for console, but at the same time, I don't know how else to detect if a player has clicked on a certain button?

1 answer

Log in to vote
1
Answered by
OfcPedroo 396 Moderation Voter
7 years ago
Edited 7 years ago

That really depends on what you're trying to do. I recommend using MouseButton1Click, MouseButton2Click, MouseButton1Up, MouseButton1Down, MouseButton2Up and even MouseButton2Down when using a single, simple function, for example, changing a team color, tweening a gui's size or position, changing texts, destroying and cloning things...

But UIS (UserInputService) should be used when making more complicated functions, and SPECIALLY when using keys on the keyboard.

Here, the first option (of the MouseButton1Click, etc.) should only be used when you're making a function with the MOUSE, and more basic functions. But, if you're trying to make functions with KEYS of the keyboard, which are normally more complex, I suggest you totally to, instead, use UIS.

EXAMPLES:

Using MouseButton1Click:

script.Parent.MouseButton1Click:connect(function() --script.Parent is a TextButton
    script.Parent.Text = "The button was pressed now."
end)

When using UIS:

function onKeyPress(inputObject, gameProcessedEvent) --if key is pressed
    if inputObject.KeyCode == Enum.KeyCode.R then --if "R" key is pressed
        print("R was pressed") --Code, which is the example **print "R was pressed".**
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress) --Get UIS and check if player inputs any key (on the keyboard)

EDIT

If you want the UIS and pressing A on the console, simply set a function and then call it, like this:

function click()
print("CODE GOES HERE!")
end

function onKeyPress(inputObject, gameProcessedEvent) --if key is pressed
    if inputObject.KeyCode == Enum.KeyCode.R then --if "R" key is pressed
        click() --Call click() function, which was set there up.
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress) --Get UIS and check if player inputs any key (on the keyboard)

--And here you should put the "Clicking on A" function on the console... and then call the click() function, like how I did there up.

Hope I could help, and if you found this useful, please mark as the solution!

As always, keep the good scripting! :P

0
How can I make it so clicking a button with the mosue will do the exact same thing as pressing A on the console, instead of having to put the section of code twice NinjoOnline 1146 — 7y
0
You can't I'm pretty sure. I mean, you can, but you have to write the UIS script (the second one) and additionally add the "a" on console. Tho, I just edited the answer rn, take a look. OfcPedroo 396 — 7y
0
Btw, can you mark this answer as the correct one? =] #rep OfcPedroo 396 — 7y
Ad

Answer this question