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

How can I make a MouseButton fire an event after a KeyCode is pressed?

Asked by 4 years ago
Edited 4 years ago
  • I put urgent in the title because I really need help with this, and help is very much appreciated, and just so whoever reads this knows, it is a local script.

  • EDIT: UIS Is UserInputService. which seems kind of obvious I guess but I should probably provide as much information as I can

So I've got some code here, and I was wondering how I would make it so after the KeyCode is pressed, but only if the KeyCode is pressed first, and then the left mouse button is clicked, it would fire the event, instead of just firing the event when the KeyCode is pressed.

UIS.InputBegan:Connect(function(key)
  if key.KeyCode == Enum.KeyCode.Q then
    -- I'm assuming the left click/mousebutton1 thing would go here
     game.ReplicatedStorage.Events.Wall:FireServer()
  end
end)

2 answers

Log in to vote
0
Answered by 4 years ago

Just add variable to check if "Q" is pressing

Use UserInputType for Mouse and use UserInputState for start and stop pressing

local UIS = game:GetService("UserInputService")

local check = false

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        if input.UserInputState == Enum.UserInputState.Begin then --start press
            check = true
        elseif input.UserInputState == Enum.UserInputState.End then --stop press
            check = false
        end
    end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if input.UserInputState == Enum.UserInputState.End then --only fire when mouse click not mouse down
            if check == true then
                game.ReplicatedStorage.Events.Wall:FireServer()
            end
        end
    end
end)
Ad
Log in to vote
0
Answered by 4 years ago

Then if it goes there it would be this

UIS.InputBegan:Connect(function(key)
  if key.KeyCode == Enum.KeyCode.Q then
UIS.MouseButton1Click:connect(function()
     game.ReplicatedStorage.Events.Wall:FireServer()
  end
end)

Edit UIS to what ever it is Im not sure but accept if helps

0
Thank you @Officer_DuckyC, I should have provided more information though, UIS is UserInputService and I got it implemented into the script with no errors, but the problem is that it says MouseButton1Click is not a valid member of UserInputService, any idea on how I could get around that? Iroxxigar 24 — 4y

Answer this question