How do i make function happen when you press a key?

Im trying to make it where when you press a key (The Spacebar for example) for a function to happen.

I’ve tried

if input.KeyCode == Enum.KeyCode.Space then
	[Code Here]
end

But when I run it, nothing happens, am I doing something wrong, I’m new to this.

5 Likes

Is your code in StarterGui or StarterPlayerScripts? Because the line of code works for me
image

local key = game:GetService("UserInputService")

key.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		print("test")
	end
end)
7 Likes

its working now, but when the code i put goes into a affect it doesn’t show on screen, it’s a startergui that is supposed to go away when you press space, it only changes in properties and not on the actual screen

3 Likes