How to detect space key input?

I’m making an game similar to “The Henry Stickim Collection”, and I need to detect the input for the start menu, how would I detect when the player presses the space key?

I tried getting help from the developer hub, but was not sucsessful.

mouse.KeyDown:connect(function(key)
    if key:byte() == 32 then
       
    end
end)
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(i)
 if i.KeyCode == Enum.KeyCode.Space then
  -- paste your code here
 end
end)
3 Likes

Just use UserInputService Read this document here for more info UserInputService | Documentation - Roblox Creator Hub

local InputService = game:GetService("UserInputService")
InputService.InputBegan:Connect(function(Input))
if Input.KeyCode == enum.KeyCode.Space then
-------------whatever you want to do----------
end
end)

You could use UserInputService or ContextActionService

UserInputService:

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.Space then 
		print("Space!")
	end
end)

ContextActionService:

local ContextActionService = game:GetService("ContextActionService")

function Space(ActionName,InputState,InputObject)
	if InputState == Enum.UserInputState.Begin then -- Will only run the code when the user presses the key, remove this line if you want it to run the code when the user presses and releases the key.
		print("Space!")
	end
end

--[[
	1st Parameter: ActionName
	2st Parameter: FunctionToBind
	3rd Parameter: CreateTouchButton
	4rd Parameter: InputType
]]

ContextActionService:BindAction("Space",Space,false,Enum.KeyCode.Space)
4 Likes

I’d like to give additional information that Mouse.Keydown is deprecated and you should use UserInputService or ContextActionService over it.

4 Likes