How would i use userinputservice to activate a gui button?

im trying to use the space bar while also being able to click a button and run a function? i thought i could do something like

if input == spacebar then button.Activated

not actually that im just too lazy to type it correctly
but that obviously doesnt work

1 Like

why not just run the script inside the button when you press spacebar

could you explain that more i dont really understand wym

Can you elaborate what you mean here? I don’t quite understand what your going for.

Are you trying to activate your if statement when the button is clicked while spacebar is held?

Or are you trying to use the spacebar whilst clicking on the button (If this is the case, then its likely something in the script as I know of nothing like that in the engine)

im trying to make it so like u can use either the space bar to press the button or clicking it

local function Thing()
    print("Button or Spacebar Pressed")
end

Button.Activated:Connect(function()
    Thing()
end)

game:GetService("UserInputService").InputBegan:Connect(function(Input, Processed)
    if Input.KeyCode == Enum.KeyCode.Space and not Processed then 
       Thing() 
    end
end)

something like this

1 Like

tried this and it didnt work thats why i came here i have no clue dude

I admit I’m an amatuer, so somebody probably has a better solution than I do, however,

button.Activated

and

button.MouseButton1Click --- Or 2, depending on what your going for

are both events, and so is the method of getting spacebar from UserInputService.
Whilst there are other ways, I would recommend either:
Having 2 separate activation sources, eg

button.Mouse1Click:Connect(Function()
--- yadda yadda
)
userinputserver.InputBegan:Connect(Function(input)
 ----- yadda yadda
)

or Having the UserInputService event inside of the Mouse1Button Event.

I forgot to elaborate about those functions being events.
Unfortunately, you cant have events in an if statement, atleast one that leads into a function.
Maybe you can find info on that elsewhere, I havent messed much with stuff like this.

could you reply with your script?

Heres a good example on how to use userinputservice that i took from developer.roblox.com:


local UserInputService = game:GetService("UserInputService")
 

local function onInputBegan(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		-- your code to open the gui here
	end
end
 
UserInputService.InputBegan:Connect(onInputBegan)

The script they provided would work though, you just likely used it incorrectly.