You could use UserInputService or ContextActionService
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.Space then
print("Space!")
end
end)
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)