Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

  • 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

✓ Accepted answer

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)

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

1 comment

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.